我试图用这个承诺从数据库发回用户数据,但它最终发送一个空对象的任何想法为什么?我在想它是因为屈服关键不存在。但是当我添加yield键时会抛出错误
The keyword 'yield' is reserved var user = yield User.findBy('key', key);
代码:
LookupHelper.js
'use strict';
const User = use('App/Model/User');
class LookupHelper {
// Looks up a user based on the key parameter and responds with the user object.
static *searchAccountKey(key) {
return new Promise((resolve,reject) => {
var user = yield User.findBy('key', key);
if (!user) {
reject({
status:'Not Found',
message: 'Unable to find the user with that key please try the email'
})
} else {
resolve(user);
}
});
}
}
module.exports = LookupHelper;
UsersController.js查找方法
* lookup(req, res) {
const account = req.params('account');
if (Validation.isNaN(account.account)) {
Lookup.searchAccountKey(account.account).then(function(user) {
console.log(user);
}).catch(function(err){
console.log("ERROR")
});
} else {
// Email lookup alternative
}
}