L'esempio quiCome posso eseguire il controllo dei tipi in MongoDB?
http://mongoosejs.com/docs/populate.html
Fornisce il codice seguente
var story1 = new Story({
title: "A man who cooked Nintendo"
, _creator: aaron._id
});
_creator è sopra definito come segue
_creator : { type: Schema.ObjectId, ref: 'Person' }
Se modifico il codice di seguito
var story1 = new Story({
title: "A man who cooked Nintendo"
, _creator: {name: 'test'}
});
Sembra felicemente inserire i dati in MongoDB.
{ "title" : "A man who cooked Nintendo", "_creator" : { "name" : "test" }, "_id" : ObjectId("4fb7a55315c5f2de07000002"), "fans" : [ ] }
Come posso rilevare l'errore prima dell'inserimento? Vorrei verificare che non sia solo un ObjectId ma anche che corrisponda ad una Persona valida.
questo dà 'TypeError: Object function ObjectId (chiave, opzioni) {SchemaType.call (this, key, options, 'ObjectID'); } non ha un metodo 'isValid'' – Tom
credo che hanno cambiato in: 'mongoose.Types.ObjectId.isValid()' – herbyme
aggiornate, grazie @herbyme –