Skip to content

Instantly share code, notes, and snippets.

@Nepoxx
Last active November 19, 2015 22:47
Show Gist options
  • Save Nepoxx/d18d10df10f33e62b833 to your computer and use it in GitHub Desktop.
Save Nepoxx/d18d10df10f33e62b833 to your computer and use it in GitHub Desktop.
'use strict'
const router = require('express').Router()
const Project = require('../models/project')
const Build = require('../models/build')
// require('mongoose').Promise = require('bluebird') this is already done elsewhere
router.param('projectname', (req, res, next, id) => {
if (!id) {
let err = new Error('Project name cannot be empty.')
err.status = 400
next(err)
return
}
Project
.findOne({pn: id})
.then(project => {
if (!project) {
let err = new Error('The given project name was not found.')
err.status = 404
throw err
}
req.project = project
next()
})
.catch(next)
})
// -- Routes ------
router.get('/', (req, res, next) => {
Project
.find()
.exec()
.then(projects => res.end(JSON.stringify(projects)))
.catch(next)
})
router.get('/:projectname', (req, res, next) => {
res.json(req.project)
})
router.get('/:projectname/builds', (req, res, next) => {
Build
.find({pn: req.project.pn})
.exec()
.then(builds => res.json(builds))
.catch(next)
})
module.exports = router
Warning: a promise was created in a handler but was not returned from it
at Query.exec (/src/node_modules/mongoose/lib/query.js:2125:10)
at /src/routes/projectCtrl.js:50:6
at Layer.handle [as handle_request] (/src/node_modules/express/lib/router/layer.js:95:5)
at next (/src/node_modules/express/lib/router/route.js:131:13)
at Route.dispatch (/src/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/src/node_modules/express/lib/router/layer.js:95:5)
at /src/node_modules/express/lib/router/index.js:277:22
at param (/src/node_modules/express/lib/router/index.js:349:14)
at paramCallback (/src/node_modules/express/lib/router/index.js:401:21)
at /src/routes/projectCtrl.js:27:7
at processImmediate [as _immediateCallback] (timers.js:383:17)
From previous event:
at Query.then (/src/node_modules/mongoose/lib/query.js:2155:22)
at /src/routes/projectCtrl.js:19:6
at paramCallback (/src/node_modules/express/lib/router/index.js:404:7)
at param (/src/node_modules/express/lib/router/index.js:384:5)
at Function.process_params (/src/node_modules/express/lib/router/index.js:410:3)
at next (/src/node_modules/express/lib/router/index.js:271:10)
at Function.handle (/src/node_modules/express/lib/router/index.js:176:3)
at router (/src/node_modules/express/lib/router/index.js:46:12)
at Layer.handle [as handle_request] (/src/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/src/node_modules/express/lib/router/index.js:312:13)
at /src/node_modules/express/lib/router/index.js:280:7
at Function.process_params (/src/node_modules/express/lib/router/index.js:330:12)
at next (/src/node_modules/express/lib/router/index.js:271:10)
at /src/app.js:34:3
at Layer.handle [as handle_request] (/src/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/src/node_modules/express/lib/router/index.js:312:13)
at /src/node_modules/express/lib/router/index.js:280:7
at Function.process_params (/src/node_modules/express/lib/router/index.js:330:12)
at next (/src/node_modules/express/lib/router/index.js:271:10)
at /src/app.js:27:3
at Layer.handle [as handle_request] (/src/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/src/node_modules/express/lib/router/index.js:312:13)
at /src/node_modules/express/lib/router/index.js:280:7
at Function.process_params (/src/node_modules/express/lib/router/index.js:330:12)
at next (/src/node_modules/express/lib/router/index.js:271:10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment