2017-03-07 6 views
1

したがって、私はコントローラ上のいくつかのコードを減らすために別のヘルパーを作成しています。だから私は自分のデータベース内のユーザを検索するのに役立つLookupというクラスを作成し、searchAccountKey(key、c​​allback)を作成しました。だから、私はこのメソッドを利用するたびに動作するようですが、ユーザオブジェクトはユーザの代わりに何も返しません。Lookupクラスメソッドは、ユーザーデータの代わりに空のオブジェクトを返します

私はこれが収量のために起こっていると思っていますが、私が使用している歩留りを使用すると、私にエラーが発生します。

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; 

がUserController(ライン44)

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

編集:私は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; 
+1

本当にES6を使用している場合は、この種の非同期制御フローのコールバックではなく、プロミスを使用する必要があります。 – gyre

+0

私は約束に慣れていません。これに関するいくつかの良い文書にリンクすることができますか? – Ethan

+0

https://www.promisejs.org/ – gyre

答えて

2

キーワードyieldはジェネレータ内でのみ使用できます。 searchAccountKeyは現在通常の機能です。関数の名前の前に*を使用して、generatorにする必要があります。

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

この変更の後、あなたはまた、yieldLookup.searchAccountKeyを呼び出す必要があります。

yield Lookup.searchAccountKey(...) 
関連する問題