I have this mongoose schema:
// ------- creating active_rooms model -------
var active_rooms_schema = mongoose.Schema({
room_name: String,
users: [String]
});
var active_rooms = mongoose.model('active_rooms', active_rooms_schema);
the DB looks like this:
[{"_id":"586b8eeeebb48c65bcbbc5f3","room_name":"my_roome","__v":0,"users":["sam","sally"]}]
pretty strightforward. I have a chat room and people (array of user to that room). I want to remove a certain person once he/she exits the room.
therfore, I wrote this code on my server:
remove_user_from_room: function (user_name, room_name) {
console.log("remove_user_from_room: " + room_name + ", user: " + user_name);
active_rooms.update( {'room_name': room_name }, { $pullAll: {'users': [user_name] } } )
},
however, this code doesn't remove a user from the array. it doesn't change the array in my DB.
where is my mistake?
Thanks
The problem was of lack sync, I needed to use promises.. the code above is correct.
Why are you using $pullAll, if you want to remove a single person only. Use $pull
remove_user_from_room: function (user_name, room_name) {
console.log("remove_user_from_room: " + room_name + ", user: " + user_name);
active_rooms.update( {'room_name': room_name }, { $pull: {'users': user_name } } )
},