我正在开发一个使用mysql
和sequelize
包的express 4应用程序。Sequelize ORM使用promises从数据库中获取数据。我试图在路由器中获取数据并发送JSON响应。当我尝试使用then
回调函数链接promise和res.json
时,控制台会报错,显示Unhandled rejection TypeError: Cannot read property 'get' of undefined
// This works
employeeRouter.get("/:id", function(req, res){
Employee.findById(req.params.id).then(function(data){
res.json(data);
});
});
// Replacing above code with following doesn't work
employeeRouter.get("/:id", function(req, res){
Employee.findById(req.params.id).then(res.json);
});
错误堆栈:
Unhandled rejection TypeError: Cannot read property 'get' of undefined
at json (D:\Workstation\DataPro\CountryStats\node_modules\express\lib\response.js:241:21)
at tryCatcher (D:\Workstation\DataPro\CountryStats\node_modules\bluebird\js\release\util.js:16:23)
at Promise._settlePromiseFromHandler (D:\Workstation\DataPro\CountryStats\node_modules\bluebird\js\release\promise.js:504:31)
at Promise._settlePromise (D:\Workstation\DataPro\CountryStats\node_modules\bluebird\js\release\promise.js:561:18)
at Promise._settlePromise0 (D:\Workstation\DataPro\CountryStats\node_modules\bluebird\js\release\promise.js:606:10)
at Promise._settlePromises (D:\Workstation\DataPro\CountryStats\node_modules\bluebird\js\release\promise.js:685:18)
at Async._drainQueue (D:\Workstation\DataPro\CountryStats\node_modules\bluebird\js\release\async.js:138:16)
at Async._drainQueues (D:\Workstation\DataPro\CountryStats\node_modules\bluebird\js\release\async.js:148:10)
at Immediate.Async.drainQueues [as _onImmediate] (D:\Workstation\DataPro\CountryStats\node_modules\bluebird\js\release\async.js:17:14)
at processImmediate [as _immediateCallback] (timers.js:383:17)
models/employee.js
var Sequelize = require('sequelize'),
sequelize = require('../db-connect/sequelize');
(function(){
// Use Strict Linting
'use strict';
// Define Sequalize
var Employee = sequelize.define('employee', {
empNo: { field: 'emp_no', type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true },
birthDate: { field: 'birth_date', type: Sequelize.DATE },
firstName: { field: 'first_name', type: Sequelize.STRING },
lastName: { field: 'last_name', type: Sequelize.STRING },
gender: { field: 'gender', type: Sequelize.ENUM('M', 'F') },
hireDate: { field: 'hire_date', type: Sequelize.DATE },
});
// Export
module.exports = Employee;
}());
db-connect/sequelize.js
var Sequelize = require('sequelize');
(function(){
// Use Strict Linting
'use strict';
// Sequalize Connection
var sequelize = null;
// Create Sequalize Connection
if(!sequelize){
sequelize = new Sequelize('employees', 'root', '', {
host: 'localhost',
dialect: 'mysql',
define: {
timestamps: false
}
});
}
module.exports = sequelize;
}());
routes/employees.js
var express = require('express'),
Employee = require('../models/employee');
(function(app){
// Use Strict Linting
'use strict';
// Create Router
var employeeRouter = express.Router();
// Home Page
employeeRouter.get("/", function(req, res){
res.json({employees: ['all']});
});
// Get Specific Employee
employeeRouter.get("/:id", function(req, res, next){
Employee.findById(req.params.id).then(function(data){
res.json(data);
});
});
// ----------------------------------
// Export
// ----------------------------------
module.exports = employeeRouter;
}());
.bind()
的polyfill 此处以了解其工作原理的示例。 - jfriend00