2017-06-24 6 views
1

nodejsのqライブラリを使用しようとしましたが、私はQ.fcallを使い、次のエラーがあります。q(約束)では、未定義のプロパティ 'apply'を読み取ることができませんか?

file_path/node_modules/q.js:155 
      throw e; 
      ^
TypeError: Cannot read property 'apply' of undefined 
at Promise.apply (/Users/name/Desktop/Programming/video_tutorial_archive/node_modules/q/q.js:1185:25) 
at Promise.promise.promiseDispatch (/Users/name/Desktop/Programming/video_tutorial_archive/node_modules/q/q.js:808:41) 
at /Users/name/Desktop/Programming/video_tutorial_archive/node_modules/q/q.js:1411:14 
at runSingle (/Users/name/Desktop/Programming/video_tutorial_archive/node_modules/q/q.js:137:13) 
at flush (/Users/name/Desktop/Programming/video_tutorial_archive/node_modules/q/q.js:125:13) 
at _combinedTickCallback (internal/process/next_tick.js:73:7) 
at process._tickCallback (internal/process/next_tick.js:104:9) 

次は私のコードです:

app.get('/',function(req,res){ 

Q.fcall(sql_query.select_comment(con,comment)) 
.then(function(){ 

    var deferred=Q.defer(); 
    con.query("SELECT * FROM "+table_video, function (err, result) { 
     console.log("step 2 finished"); 
     console.log("comment is "+comment); 
      // if (err) throw err; 
      query_result=result; 
      // deferred.resolve(); 

     // console.log(query_result); 
    }) 
    // return deferred.promise; 

}).then(function(){ 
    console.log("step 3 finished"); 
    console.log("comment is "+comment); 
    console.log(query_result); 

    res.render('index',{ 
       result:query_result, 
       comment:comment.comment 
    }); 
}).done(); 

}); 

私はQ.deferでそれを解決することができますが、私は代わりにfcall使用したいです。それは、より洗練されたものであり、延期されたすべての賛成も、遅延もない。

「原因:未定義のapply 'を読み取ることができません」というエラーが発生していますか?それを修正する方法は?あなたがsql_query.select_comment(con, comment)の戻り値が関数ではないので、それは.apply()を見つけることができませんsql_query.select_comment.call(this, con, comment)

で行うのと同じように、Q.fcall(sql_query.select_comment, con, comment)

答えて

0

は、私はあなたがのparamsを分離することになっていると思います。

2

まず、関数をQ.fcall()に渡す必要があります。あなたは彼の明らかに機能ではないsql_query.select_comment(con,comment)からの返品結果を渡しています。

Q.fcall()を正しく使用するには、呼び出す関数として最初の引数を渡します。次の引数は、その関数に渡す引数です。また、select_commentを依然としてsql_queryにバインドしたい場合は(それが必要かどうかわかりません)、.bind()を使用すると安全です。あなたはこのようなことすべて一緒に置くことができます:

Q.fcall(sql_query.select_comment.bind(sql_query), con, comment) 

sql_query.select_comment()からの戻り値がundefinedあるので、あなたがCannot read property 'apply' of undefinedを取得し、エラーがQ.fcall()が引数を添付することに.apply()を使用しようとするので、それはそのエラーをスローです。

また、お約束がcon.query("SELECT * FROM "+table_video, function (err, result) {})が完了するのを待っていないという別のエラーがあります。最良のソリューションは、すべてのDB機能に対する約束インタフェースを使用することです。それで、.then()ハンドラの内部からcon.query()の約束を返すことができます。それは自動的に親の約束に連鎖し、物事は正しくシーケンスされます。 Q.fcall()の内部で何が起こっているのか自分の目で確かめてくださいするには

、あなたはその機能here on Githubのソースを見ることができます:

これは最終的に Q.fcall()に最初の引数に .apply()を呼び出そうとします
Q.fcall = function (object /* ...args*/) { 
    return Q(object).dispatch("apply", [void 0, array_slice(arguments, 1)]); 
}; 

関連する問題