MongoDB save()问题与对象数组

时间:2016-02-22 08:41:56

标签: javascript node.js mongodb mongoose

此代码是用户之间发送消息的发布请求。当消息不是现有消息字符串的一部分时(即typeof existingMessageIndex === 'undefined'),它会成功。 docs.save()会导致错误("error saving outgoing message"未定义existingMessageIndex

似乎mongo并不喜欢我试图将一组对象保存为现有数组字段中的一个项目。

任何人都知道我错过了什么?

.post(function(req,res,next){
    var messageStringTitle = req.body.originatingTitle !== ''?req.body.originatingTitle:req.body.messageTitle;

    var newMessage = {
        senderDisplay: req.user.displayname,
        senderUser: req.user.username,
        senderImage: req.user.profileimg,
        originatingTitle: messageStringTitle,
        title: req.body.messageTitle,
        content: req.body.messageContent
    };
    var messageSender = function(docs){
        var tempMessageArray = docs.messagein;

        for(var i = 0; i<tempMessageArray.length; i++){
            console.log("Loop has executed: " + i);
            if(tempMessageArray[i][0]){
                if(tempMessageArray[i][0].originatingTitle===messageStringTitle && tempMessageArray[i][0].senderUser===req.user.username || tempMessageArray[i][0].senderUser===req.body.messageUser){
                    var existingMessageIndex = i;
                }
            }
        }

        console.log("===================" + existingMessageIndex);
        if(typeof existingMessageIndex === 'undefined'){
            tempMessageArray.unshift([newMessage]);
            docs.messagein = tempMessageArray;
        }else{
            docs.messagein[existingMessageIndex].unshift(newMessage);
        }
        return docs;
    };


    //Update sending user
    User.findOne({username: req.user.username}, function(e, docs){
        messageSender(docs);
        console.log(docs);   //Logs exactly what I want to be saved, however saving fails
        docs.save(function(e){
            console.log("Error saving outgoing message");
        })
    });


    //Update receiving user
    User.findOne({username: req.body.messageUser}, function(e, docs){
        messageSender(docs);
        console.log(docs);
        docs.save(function(e){
            if(e){
                console.log("Error sending message within post ('/messages')");
            }
        });

    });

1 个答案:

答案 0 :(得分:0)

<强>解决方案: 需要使用db.collection.markModified(),因为架构&#39; messagein&#39;当它变成一个对象数组数组时,它被改为输入混合类型。

 //Update sending user
    User.findOne({username: req.user.username}, function(e, docs){
        messageSender(docs);
        console.log(docs);   //Logs exactly what I want to be saved, however saving fails
        docs.markModified('messagein'); //ADDED
        docs.save(function(e){
            console.log("Error saving outgoing message");
        })
    });


    //Update receiving user
    User.findOne({username: req.body.messageUser}, function(e, docs){
        messageSender(docs);
        console.log(docs);
        docs.markModified('messagein'); //ADDED
        docs.save(function(e){
            if(e){
                console.log("Error sending message within post ('/messages')");
            }
        });

注意:解决方案有效,但解释只是我的猜测,如果我错了,请纠正我。谢谢!