Ich versuche, eine Zeichenfolge an ein Array anzuhängen, das zur Identifizierung eines Benutzers in mehreren Chatrooms verwendet wird. Ich habe es versucht und kann das nicht zur Arbeit bringen. Das Protokoll ist löschte nichts als:An Array anfügen nodejs mongodb
[]
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 Struktur
{
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}}]
}
Ich tat wie du gesagt hast. Ich änderte den Code wie folgt: \t \t \t \t app.model.User.update ( \t \t \t \t {_id: req.locals.user._id}, \t \t \t \t {$ addToSet: {chatid: {mid: req.body.value}}}, \t \t \t \t Funktion (err, result) { \t \t \t \t \t if (result) \t \t \t \t \t \t console.log (Ergebnis); \t \t \t \t \t} \t \t \t \t); Danach funktioniert das Setid jetzt. Ich schätze Ihre Hilfe und fand es absolut nützlich. – Justin
I modifizierten Code als: app.model.User.find ({ "chatid": {$ elemMatch: { "_id": req.body.value}}} . -Code funktioniert nun vollständig ich wirklich zu schätzen Ihre Hilfe! Hinweis: geändert in _id. – Justin