【发布时间】:2018-09-09 18:31:45
【问题描述】:
对 Javascript 和 Node.js 来说是全新的。我正在尝试将 Sequelize 作为 ORM 开始,并做了一个简单的查询
var employeeList = Employee.findAll().then(function(result){
console.log(result.length);
console.log(result[0].employeeName);
//how do I return this to a variable with which I can do further processing
return result;
});
//do something with employeeList
employeeList[0].employeeName //cannot read property of undefined
虽然控制台日志打印出正确的名称,但employeeList 本身不包含任何数据。我尝试打印employeeList,它显示了承诺
Promise {
_bitField: 0,
_fulfillmentHandler0: undefined,
_rejectionHandler0: undefined,
_promise0: undefined,
_receiver0: undefined }
我确实浏览了 Promise 的概念,但无法获得有关如何将 Promise 的结果返回到函数外部的变量的东方示例。我认为返回结果会起到作用。我在这里错过了什么吗?我可以理解我可以在 promise 函数中处理结果。如果场景是进行两次数据库调用,然后处理两次调用的结果以返回合并结果,如何在不将结果传递给变量的情况下完成。
【问题讨论】:
标签: node.js express promise sequelize.js