0
votes

I'm trying to use MongoDB and mongoose for my little conversation app in where each user is having it's own collection but I'm getting an error when saving a new conversation:

ERROR: MongoError: insertDocument :: caused by :: 11000 E11000 duplicate key error index: chat.privatemessages.$id dup key: { : ObjectId('54f1a39ff467833f7e794636') }

My Schema:

    var chatPrivateMessagesSchema = mongoose.Schema({
    user: String,
    with:[{
        conversation_with: String,
        messages:[{
            text:String,
            created: {type: Date, default: Date.now}
        }]
    }]
});

var PrivateMessages = mongoose.model('PrivateMessages', chatPrivateMessagesSchema);

Method for saving new conversation:

socket.on ('chat', function (from, to, message, callback) {
    PrivateMessages.find ({}, function (err, users) {
        if (err) throw err;
        for (var i = 0; i < users.length; i++) {
            if (users[i].user == from) {
                var newPrivateMsg = new PrivateMessages ({_id: users[i]._id}, {
                    with: [{
                        conversation_with: to, //if conversation with "to" user does not exist create a new entry
                        messages: [{
                            text: message, //append a new message 
                            created: {type: Date, default: Date.now}
                        }]
                    }]
                });
                newPrivateMsg.save (function (err) {
                    if (err) throw err;
                });
            }
        }
    });
});

So basically the output I'm looking for should be something like this?

{
    "_id": "some id",
    "user": "John",
    "with": [{
        "conversation_with": "Maria",
        "_id": "some id",
        "messages": [{
            "text": "Hellomyfisrtmessage",
            "created": "somedate"
        }, {
            "text": "Secondmessage ",
            "created": "somedate"
        }, {
            "text": "Thirdmessage",
            "created": "somedate"
        }, ]
    }, {
        "conversation_with": "Peter",
        "_id": "some id",
        "messages": [{
                "text": "Hellomyfisrtmessage",
                "created": "somedate"
            }, {
                "text": "Secondmessage ",
                "created": "somedate"
            }, {
                "text": "Third message",
                "created": "some date"
            },

        ]
    }],

}

I'm really struggling how to insert a new entry :( Can anyone help please?

1

1 Answers

1
votes

The issue is that you're setting every PrivateMessage _id to the user's _id, don't do that!

EDIT:

socket.on ('chat', function (from, to, message, callback) {
    PrivateMessages.find ({}, function (err, users) {
        if (err) throw err;
        for (var i = 0; i < users.length; i++) {
            if (users[i].user == from) {
                users[i].with.forEach(function(with){
                    if (with.conversation_with === to ){

                        // append new message
                        with.messages.push({
                            text: message,
                            created: { type: Date, default: Date.now }
                        })
                    }
                })

                users[i].save (function (err) {
                    if (err) throw err;
                });

            }
        }
    });
});