Friday, 14 April 2017

Append to array nodejs mongodb

I am trying to append a string to an array that will be used to identify a user in multiple chatrooms. I have tried and can not get this to work. The log does not put out anything but:

[]

            if (req.body.method == "setid") {
            if(req.locals.user.chatid == null){
                req.locals.user.chatid = {};
            }

            app.model.User.update(
               { _id: req.locals.user._id },
               { $addToSet: { chatid: [{mid: req.body.value}]}  } 
            );
        } else if (req.body.method == "checkid" && req.body.value) {
            app.model.User.find({
                "chatid": req.body.value
            }, function(err, FoundUser) {
                if (err || !FoundUser) {
                    res.end("ERROR");
                } else {
                    console.log(FoundUser);
                    res.end(FoundUser[0].displayName);
                }
            });
        }

Mongo Structure

{
    email: { type: String, index: { unique: true }, required: true },
    password: { type: String },
    changeName: {type: Boolean},
    displayName: { type: String, index: { unique: true }, required: true },
    avatar: { type: String, default: '/assets/anon.png' },
    permissions: {
        chat: { type: Boolean, default: true },
        admin: { type: Boolean, default: false }
    },
    online: { type: Boolean, default: true },
    channel: { type: Types.ObjectId, ref: 'Channel', default: null }, //users channel
    banned: { type: Boolean, default: false },
    banend: {type:String, default: ''},
    subs: [{ type: Types.ObjectId, ref: 'User' }],
    about: { type: String },
    temporary: {type: Boolean, default: false},
    chatid:[{mid: {type:String}}]
}



via Justin

No comments:

Post a Comment