似乎 Mongoose 在内部做了一些非常时髦的事情。
var Foo = new mongoose.model('Foo', new mongoose.Schema({a: String, b: Number}));
var foo = new Foo({a: 'test'; b: 42});
var obj = {c: 1};
foo.goo = obj; // simple object assignment. obj should be
// passed by reference to foo.goo. recall goo
// is not defined in the Foo model schema
console.log(foo.goo === obj); // comparison directly after the assignment
// => false, doesn't behave like normal JS object
本质上,每当您尝试处理 Mongoose 模型的属性时,
a) 在模型的模式中定义或
b) 定义为相同类型 (array, obj, ..) ...模型的行为甚至不像普通的 Javascript 对象。
将第4行切换为foo._doc.goo = obj
控制台输出true
。
编辑:试图重现怪异
示例 1:
// Customer has a property 'name', but no property 'text'
// I do this because I need to transform my data slightly before sending it
// to client.
models.Customer.find({}, function(err, data) {
for (var i=0, len=data.length; i<len; ++i) {
data[i] = data[i]._doc; // if I don't do this, returned data
// has no 'text' property
data[i].text = data[i].name;
}
res.json({success: err, response:data});
});