Express 4 - 用promise.then链接res.json不起作用

时间:2016-06-28 20:20:25

标签: node.js express promise sequelize.js bluebird

我正在使用mysqlsequelize个套餐的快递4应用。 Sequelize ORM使用promises从数据库中获取数据。我试图在路由器中获取数据并发送json响应。当我尝试使用then链接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)

模型/ 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-连接/ 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;

}());

路由/ 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;

}());

1 个答案:

答案 0 :(得分:12)

当您将res.json作为函数传递时,res对象会丢失,因此当json()执行时,它没有对象,您会看到错误。您可以使用.bind()

来解决这个问题
employeeRouter.get("/:id", function(req, res){
   Employee.findById(req.params.id).then(res.json.bind(res));
});

这将确保在执行方法时res对象与您的方法保持一致。如上所述使用.bind()基本上与:

相同
employeeRouter.get("/:id", function(req, res){
   Employee.findById(req.params.id).then(function(data) {
       return res.json(data);
   });
});

实际上,.bind()实际上创建了一个存根函数,就像上面例子中的匿名函数一样。它只是为你而不是让你这样做。

再举一个示例,我们假设您有两个单独的res个对象,res1res2来自两个不同的请求。

var x = res1.json;
var y = res2.json;

console.log(x === y);    // true, no association with either res1 or res2 any more

这是因为引用res1.json只会引用.json方法。它使用res1来获取该方法(从res1原型获取,但是它具有方法,它只是指向该方法的指针,并且不再与包含该对象的对象关联因此,当你将res.json传递给一个函数时,你没有得到res的附件。然后当你通过res.json的函数去实际调用你的函数时,它就像调用它一样这样:

var z = res.json;
z();

并且,当调用z()时,this内的json值最终为undefined,并且与res对象没有任何关联。使用.bind()创建一个存根函数,将其称为res.json(...)以保持与对象的连接,并确保在执行方法时正确设置this