【问题标题】:In q(promise), Cannot read property 'apply' of undefined?在 q(promise) 中,无法读取未定义的属性“应用”?
【发布时间】:2017-11-28 05:35:53
【问题描述】:

我尝试使用nodejs的q库,我使用Q.fcall然后出现如下错误。

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 代替。它更干净,没有所有的 deferred.promise 和 deferred.resolve。

是什么导致错误“无法读取未定义的属性'应用'”?以及如何解决?

【问题讨论】:

    标签: node.js promise q


    【解决方案1】:

    首先,您必须将一个函数传递给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)
    

    您得到Cannot read property 'apply' of undefined 的错误是因为sql_query.select_comment() 的返回值是undefined,所以当Q.fcall() 尝试在其上使用.apply() 附加参数时,它会抛出该错误。

    您还有另一个错误,即您的外部承诺没有等待con.query("SELECT * FROM "+table_video, function (err, result) {}) 完成。最好的解决方案是对所有数据库函数只使用一个 Promise 接口。然后,您可以从 .then() 处理程序内部返回 con.query() 的承诺,它将自动链接到父承诺,并且事情将正确排序。

    要亲自了解Q.fcall() 内部发生了什么,您可以查看该函数here on Github 的源代码:

    Q.fcall = function (object /* ...args*/) {
        return Q(object).dispatch("apply", [void 0, array_slice(arguments, 1)]);
    };
    

    这最终会尝试在Q.fcall() 的第一个参数上调用.apply()

    【讨论】:

      【解决方案2】:

      我认为你应该分开参数:Q.fcall(sql_query.select_comment, con, comment),就像你对sql_query.select_comment.call(this, con, comment)做的那样

      找不到.apply(),因为sql_query.select_comment(con, comment)的返回值不是函数。

      【讨论】:

        猜你喜欢
        • 2016-07-28
        • 1970-01-01
        • 2014-11-26
        • 2018-01-15
        • 2018-06-02
        • 2017-06-22
        • 2016-03-04
        • 2016-12-07
        • 1970-01-01
        相关资源
        最近更新 更多