查找 class 方法 returns 空对象而不是用户数据

Lookup class method returns empty object instead of user data

所以,我正在创建不同的助手来减少控制器上的一些代码。所以我创建了一个名为 Lookup 的 class 来帮助我在我的数据库中搜索用户,并且我创建了一个 searchAccountKey(key, callback)。因此,每当我使用此方法时,它似乎都有效,但是用户对象 returns 什么都没有,而不是用户。

我怀疑这是因为 yield 而发生的,但是当我使用 yield 时它给了我一个错误。

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;

用户控制器(第 44 行)

Lookup.searchAccountKey(account.account, function(user) {
    return console.log(user);
});

编辑:每当我把 yield 放在 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 目前是一个正常的功能。您需要在函数名称前使用 * 使其成为 generator.

static * searchAccountKey (key, callback) {
  const user = yield User.findBy('key', key)
  // ...
}

此更改后,您还需要使用 yield 调用 Lookup.searchAccountKey

yield Lookup.searchAccountKey(...)