查找类方法返回空对象而不是用户数据
所以,我创build了不同的帮助器来减less我的控制器上的一些代码。 所以我创build了一个名为Lookup的类来帮助我search数据库中的用户,并创build了一个searchAccountKey(key,callback)。 所以,每当我使用这个方法,它似乎工作,但用户对象返回,而不是用户。
我怀疑这是由于收益率,但是当我使用收益率时,它给了我一个错误。
LookupHelper.js
'use strict'; const User = use('App/Model/User'); class LookupHelper { // Grab the user information by the account key static searchAccountKey(key, callback) { const user = User.findBy('key', key) if (!user) { return callback(null) } return callback(user); } } module.exports = LookupHelper;
UsersController(第44行)
Lookup.searchAccountKey(account.account, function(user) { return console.log(user); });
编辑:每当我把产量infront的User.findBy()
The keyword 'yield' is reserved const user = yield User.findBy('key', key)
码:
'use strict'; const User = use('App/Model/User'); class LookupHelper { // Grab the user information by the account key static searchAccountKey(key, callback) { const user = yield User.findBy('key', key) if (!user) { return callback(null) } return callback(user); } } module.exports = LookupHelper;
关键字yield
只能在生成器中使用。 searchAccountKey
目前是一个正常的function。 你需要在函数的名字之前使用*
来使它成为一个生成器 。
static * searchAccountKey (key, callback) { const user = yield User.findBy('key', key) // ... }
在这个改变之后,你也需要调用Lookup.searchAccountKey
。
yield Lookup.searchAccountKey(...)