Quando provo a salvare il mio documento, viene visualizzato un errore VersionError: No matching document found
, simile alla domanda this SO.Mangusta: l'incremento del numero di versione dei miei documenti non funziona e ricevo un errore di versione quando provo a salvare
Dopo aver letto il post del blog this, sembra che il problema riguardi il controllo delle versioni del mio documento. Che sto scherzando con un array e quindi ho bisogno di aggiornare la versione.
Tuttavia, chiamare document.save()
non funziona per me. Quando disconnetto il documento prima e dopo la chiamata a save()
, document._v
è la stessa cosa.
Ho anche provato a fare document._v = document._v++
che non ha funzionato.
Codice
exports.update = function(req, res) {
if (req.body._id) { delete req.body._id; }
User.findById(req.params.id, function(err, user) {
if (err) return handleError(res, err);
if (!user) return res.send(404);
var updated = _.extend(user, req.body); // doesn't increment the version number. causes problems with saving. see http://aaronheckmann.blogspot.com/2012/06/mongoose-v3-part-1-versioning.html
console.log('pre increment: ', updated);
updated.increment();
// updated._v = updated._v++;
console.log('post increment: ', updated);
updated.save(function(err) {
if (err) return handleError(res, err);
return res.json(200, user);
});
});
};
uscita
pre increment: { _id: 5550baae1b571aafa52f070c,
provider: 'local',
name: 'Adam',
email: '[email protected]',
hashedPassword: '/vahOqXwCwKQKtcV3KBQeFge/YB0xtqOj+YDyck7gzyALA/IP7u7BfqQhlVHBQT26//XfBTkaOCK2bQXg65OzA==',
salt: 'MvzXW7D4xuyGQBJNeFRoUg==',
__v: 32,
drafts: [],
starredSkims: [],
skimsCreated: [ 5550cfdab8dcacd1a7892aa4 ],
role: 'user' }
post increment: { _id: 5550baae1b571aafa52f070c,
provider: 'local',
name: 'Adam',
email: '[email protected]',
hashedPassword: '/vahOqXwCwKQKtcV3KBQeFge/YB0xtqOj+YDyck7gzyALA/IP7u7BfqQhlVHBQT26//XfBTkaOCK2bQXg65OzA==',
salt: 'MvzXW7D4xuyGQBJNeFRoUg==',
__v: 32,
drafts: [],
starredSkims: [],
skimsCreated: [ 5550cfdab8dcacd1a7892aa4 ],
role: 'user' }
"Il versioning è stato implementato per mitigare doc.save() in base alla progettazione (non Model.update ecc.)" - Non capisco cosa significhi. "confirmed-bug" - sembra che stia parlando di 'update' e' findByIdAndUpdate', ma non sto facendo nessuno di questi (ho usato la parola "update" per errore nella mia domanda, ma il mio codice mostra che I ' m chiamando 'save()') –