Sto tentando di aggiornare un documento in una raccolta (unità) utilizzando la GUI e dopo che si aggiorna Voglio aggiornare il valore (user.Units che è una matrice dei nomi di Unità) in collezione (utenti). Se la lunghezza dell'array è solo di 1 elemento, viene aggiornata e visualizzata nel database e tutto funziona correttamente, ma quando Array of Units ha più di un elemento, cerco di aggiornarlo attraverso un ciclo for, mostra che viene aggiornato ma quando Controllo il database che non è ancora aggiornato.Mongoose save() non sta aggiornando il valore in una matrice nel documento di database
Non riesco davvero a capire perché non aggiorna il database quando aggiorno il valore attraverso un ciclo.
intero modificare e aggiornare la funzione: -
edit_unit: function (req, res, next) {
var Data = req.body;
Client_data.Unit.findById(req.params.unitId, function (err, unit) {
var error = false;
if (err) {
error = err;
} else if (!unit) {
error = "FATAL: unable to look up Unit #" + req.params.unitId;
} else {
switch(req.body.name) {
case 'Icon':
var Icon = unit.Icon;
User.find({"Units":Icon}, function (err, users) {
if (err)
console.log(err);
users.forEach(function (u) {
if (u.Units.length > 1) {
for (var i = 0; i <= u.Units.length; i++) {
if(u.Units[i] == Icon) {
u.Units[i] = req.body.value;
}
}
}
else {
u.Units = req.body.value;
}
u.save(u);
});
});
unit[req.body.name] = req.body.value;
break;
case 'Description':
unit[req.body.name] = req.body.value;
break;
default:
unit[req.body.name] = req.body.value;
break;
}
var data = JSON.stringify(req.body);
unit.save();
res.writeHead(200, {
'Content-Length': data.length,
'Content-Type': 'application/json'
});
res.end(data);
}
});
}
req.body: -
{ name: 'Icon',
value: 'Health Utility 22c',
pk: '5395ed107cd92dc40eaafb56'
}
schema utente: -
var userSchema = mongoose.Schema({
UserName: { type: String, required: true },
Password: { type: String },
FirstName: { type: String, required: true },
LastName: { type: String, required: true },
CompanyName: { type: String },
PhoneNumber: { type: Number },
StartDate: { type: Date, required: true },
EndDate: { type: Date, required: true, default: new Date('9999-12-12') },
ClientID: { type: ObjectId, ref: 'Client', default: null },
DepartmentID: { type: ObjectId, ref: 'Department' },
ManagerID: { type: ObjectId, ref: 'User', default: null},
Units: [ { type: String, required: true } ],
UserList: { type: Array, default:[]},
Access: [{ type: String, enum: ['DEMO', 'USER','MANAGER','ADMINISTRATOR','OWNER']}],
Credentials: { type: String },
AFTE: { type: Number},
SessionID: { type: String, default: null }
}, { safe: true });
È possibile modificare la domanda per includere la definizione dello schema per 'Utente'? – JohnnyHK