2014-07-07 12 views
14

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 }); 
+0

È possibile modificare la domanda per includere la definizione dello schema per 'Utente'? – JohnnyHK

risposta

41

maggio essere notifica mongooose il set di dati è cambiato in questo modo:

doc.markModified('pathToYourAttribute')  

Dalla documentazione http://mongoosejs.com/docs/schematypes.html

person.anything = { x: [3, 4, { y: "changed" }] }; 
person.markModified('anything'); 

Spero che sia d'aiuto!

+1

Cosa sarebbe "PathToYourAttribute".? Mi dispiace, sono un po 'nuovo per Mangusta. –

+0

@SulemanMirza Modifica la mia risposta, la prendo dal doc – cvng

+0

@SulemanMirza Perché succede? In una situazione simile. –

3

Vedere this issue. Un modo per risolverlo sarebbe non aggiornare il tuo array tramite il classico metodo Index array. Quindi fare questo

doc.array.set(index, value); 

Invece di

doc.array[index] = value; 

anche visualizzare il FAQ e doc per maggiori dettagli.

+0

questo non funziona per una matrice di oggetti, che è la situazione dell'OP. qualche modifica per questo? – Doa

+0

questo ha funzionato per me – Mark

Problemi correlati