我在使用 Sails.JS 时遇到问题。我想将 Promise 与 Model.query() 函数一起使用(我使用 sails-mysql 适配器)。 我使用 promise 如下:
var Promise = require('bluebird');
var PatientQueryAsync = Promise.promisify(Patient.query);
但是它不起作用。它返回ReferenceError: Patient is not Define
,有人遇到同样的问题吗?非常感谢!
最佳答案
Waterline 支持 promise of the box所以没有必要做出 promise 。
Patient.findOne()
.where({ id: 5 })
.then(function(patient){
//do something
return patient.lastVisit;
}).catch(function(err){
// An error occurred
})
然而,查询并没有在 promise 中扭曲,因此需要对其进行 promise (这对我有用):
var query = Promise.promisify(Patient.query);
query('SELECT name FROM patient').then(function(results) {
//do something
});
关于javascript - 如何在 SailsJS/Waterline 中使用带有 Promise 的 Model.query(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34135364/