Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 蓝鸟承诺。每个参考错误?_Node.js_Mongodb_Promise_Node Mongodb Native_Bluebird - Fatal编程技术网

Node.js 蓝鸟承诺。每个参考错误?

Node.js 蓝鸟承诺。每个参考错误?,node.js,mongodb,promise,node-mongodb-native,bluebird,Node.js,Mongodb,Promise,Node Mongodb Native,Bluebird,我对承诺不太熟悉,不太确定.然后和.是否每个都带有贯穿整个承诺的变量 另外,我在第四行中清楚地定义了docreply,但控制台记录: 可能未处理的引用错误:未定义docReplies 我希望在repliesdsarray中的每个元素(replyID)上循环,并查找消息。然后为doc.repries数组中的每个元素查找replyID的索引,将其设置为index1。然后为doc.repries[index1]数组中的每个元素查找用户名的索引(res.locals.username),将其设置为ind

我对承诺不太熟悉,不太确定.然后和.是否每个都带有贯穿整个承诺的变量

另外,我在第四行中清楚地定义了
docreply
,但控制台记录:

可能未处理的引用错误:未定义docReplies

我希望在
repliesdsarray
中的每个元素(replyID)上循环,并查找消息。然后为
doc.repries
数组中的每个元素查找replyID的索引,将其设置为index1。然后为
doc.repries[index1]
数组中的每个元素查找用户名的索引(res.locals.username),将其设置为index2


承诺在变量声明中没有神奇的功能。
docrepress
是在第一个
回调函数中定义的。然后()
回调函数只在该函数中可用。如果希望它在多个
中可用,那么()
handler函数,然后需要在更高的范围内声明它,以便它在任何地方都可用(正常的Javascript范围规则)

或者,在某些情况下,您可以从一个承诺处理程序向另一个承诺处理程序返回数据,但听起来您并不是在尝试这样做


在任何情况下,普通的Javascript范围规则适用于所有变量声明,即使是承诺回调函数中的声明。

感谢您提供的信息。。非常感谢现在正在记录一个新错误,…感谢您的想法
Promise.each(repliesIDsArray, function(replyID){
  Models.Message.findOneAsync({'_id': req.params.message_id})
    .then(function(doc){
        var docReplies = [];
        pushReplies = docReplies.push(doc.replies);
    }).each(docReplies, function (replyIndex){
        // loop over doc.replies to find..
        // ..the index(index1) of replyID at replies[index]._id
        var index1;
        if (docReplies[replyIndex]._id == replyID) {
            index1 = replyIndex;
        }
        var docRepliesIndex1 = [];
        pushRepliesIndex1 = docRepliesIndex1.push(doc.replies[index1]);
    }).each(docRepliesIndex1, function (usernameIndex){
        // loop over doc.replies[index1].to and find..
        // ..the index(index2) of res.locals.username at replies[index1].to[index2]
        var index2;
        if (docRepliesIndex1.to[usernameIndex].username === res.locals.username) {
            index2 = usernameIndex;
        }
    }).then(function(index1, index2) {
        console.log('index1 = ' + index1);
        console.log('index2 = ' + index2);
        doc.replies[index1].to[index2].read.marked = true;
        doc.replies[index1].to[index2].read.datetime = req.body.datetimeRead;
        doc.replies[index1].to[index2].updated= req.body.datetimeRead;
        doc.markModified('replies');
        var saveFunc = Promise.promisify(doc.save, doc);
        return saveFunc();
        console.log('doc saved');
    }).then(function(saved) {
        console.log("Success! doc saved!");
        console.log("Sending saved doc:");
        res.json(saved);
    }).catch(Promise.OperationalError, function(e){
        // handle error in Mongoose findOne + save
        console.error("unable to save because: ", e.message);
        console.log(e);
        res.send(e);
        throw err;
    }).catch(function(err){
        // would be a 500 error, an OperationalError is probably a 4XX
        console.log(err);
        res.send(err);
        throw err; // this optionally marks the chain as yet to be handled
    });
})