简体   繁体   English

MongoDB save()问题与对象数组

[英]MongoDB save() issue with array of objects

This code is a post request for sending messages between users. 此代码是用于在用户之间发送消息的后请求。 It succeeds when the message is not part of an existing message string (ie: typeof existingMessageIndex === 'undefined' ). 当消息不是现有消息字符串的一部分(即: typeof existingMessageIndex === 'undefined' )时,它将成功。 The docs.save() results in the error ( "error saving outgoing message" when existingMessageIndex is not undefined. docs.save()导致错误(当不existingMessageIndex docs.save()"error saving outgoing message"

It appears that mongo doesn't like that I'm trying to save an array of objects as one item in the existing array field. 似乎mongo不喜欢我试图将对象数组另存为现有array字段中的一项。

Anyone know what I'm missing? 有人知道我想念什么吗?

.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')");
            }
        });

    });

Solution: Need to use db.collection.markModified() because the schema 'messagein' was changed to type mixed when it became an array of arrays of objects. 解决方案:需要使用db.collection.markModified()因为架构“ messagein”在变成对象数组时已更改为混合类型。

 //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')");
            }
        });

Note: The solution worked, however the explanation is just my guess, please correct me if I'm wrong. 注意:解决方案有效,但是解释只是我的猜测,如果我错了,请纠正我。 Thanks! 谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM