Lookup类方法返回空对象而不是用户数据 [英] Lookup class method returns empty object instead of user data

查看:462
本文介绍了Lookup类方法返回空对象而不是用户数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我正在创建不同的助手来减少我的控制器上的一些代码。所以我创建了一个名为Lookup的类来帮助我搜索数据库中的用户,并创建了一个searchAccountKey(key,callback)。所以,每当我使用这种方法,它似乎工作,但是用户对象没有任何东西而不是用户返回。



我怀疑这是因为收益而发生的,但是当我请使用收益给我一个错误。



LookupHelper.js

 'use strict'; 
const User = use('App / Model / User');
class LookupHelper {
//通过帐号键获取用户信息
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);
});

编辑:无论何时我输入User.findBy()



关键字'yield'被保留const user = yield User.findBy('key',key)



代码:

 'use strict'; 
const User = use('App / Model / User');
class LookupHelper {
//通过帐号键获取用户信息
static searchAccountKey(key,callback){
const user = yield User.findBy('key',key )
if(!user){
return callback(null)
}
return callback(user);
}

}

module.exports = LookupHelper;


解决方案

关键字 yield 只能在一个生成器中使用。 searchAccountKey 目前是一个正常的功能。您需要在函数名称之前使用 * 使其成为生成器

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

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

  yield Lookup.searchAccountKey(...)
/ pre>

So, I am creating different helpers to reduce some code on my controller. So I created a class called Lookup to help me search for users in my database and I created a searchAccountKey(key, callback). So, whenever I utilize this method it seems to work however the user object returns with nothing instead of the user.

I am suspecting this is happening because of yield but when I do use yield it gives me an error.

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 (line 44)

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

EDIT: Whenever I put yield infront of the User.findBy()

The keyword 'yield' is reserved const user = yield User.findBy('key', key)

Code:

'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;

解决方案

The keyword yield can be only used inside a generator. searchAccountKey is currently a normal function. You need to use * before the name of the function to make it a generator.

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

After this change you will need to call Lookup.searchAccountKey with yield also.

yield Lookup.searchAccountKey(...)

这篇关于Lookup类方法返回空对象而不是用户数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆