Node.js 当getter设置为true时,为什么在mongoose文档上调用toObject()会删除属性?
当对具有设置为Node.js 当getter设置为true时,为什么在mongoose文档上调用toObject()会删除属性?,node.js,mongoose,Node.js,Mongoose,当对具有设置为null的(嵌套)属性的文档调用toObject()时,该属性将被删除,但仅当为getters选项提供true时,才会删除,请参见下面的示例 var mongoose = require('mongoose'); const schema = new mongoose.Schema({ customer: { name: { type: String, required: false }, }, }); var Model = mongoose.mode
null
的(嵌套)属性的文档调用toObject()
时,该属性将被删除,但仅当为getters
选项提供true
时,才会删除,请参见下面的示例
var mongoose = require('mongoose');
const schema = new mongoose.Schema({
customer: {
name: { type: String, required: false },
},
});
var Model = mongoose.model('Model', schema);
var model = new Model();
model.customer = null;
console.log(model.toObject());
console.log(model.toObject({ getters: true }));
//Prints:
//{ _id: 58e00dcd71bf5916802bdb3c, customer: null }
//{ _id: 58e00dcd71bf5916802bdb3c, id: '58e00dcd71bf5916802bdb3c' }
为什么会这样?
我使用的是mongoose v.4.9.2在这两种情况下,打印第一行的代码都是相同的。使用Mongoose 4.5.2我使用v.4.9.2与4.9.2的结果相同这很奇怪,无论使用版本4.5.2还是4.9,我都有不同的打印输出。2@TalhaAwan糟糕的是,我有
getter:true
而没有getter:true
(缺少一个“s”)在上面的代码示例中,它为两种情况打印相同的输出。相同的代码为我在两种情况下打印第一行。使用Mongoose 4.5.2我使用v.4.9.2与4.9.2的结果相同这很奇怪,无论使用版本4.5.2还是4.9,我都有不同的打印输出。2@TalhaAwan糟糕的是,在上面的代码示例中,我有getter:true
和notgetter:true
(缺少一个“s”),这使得它为两种情况打印相同的输出。