不能更改delete
或library
对象的books
属性值。
Library.findOne(12).populate('books').populate('createdBy').exec(
function(err,library) {
delete library.createdBy;
//worked
delete library.name;
//worked
delete library.books;
//no effect
library.books = [];
//worked
library.books = [{a:'any val'}];
//just like library.books=[]
console.log(library);
});
我的library for books和createdBy的模型是这样的
createdBy: {
model: "createdBy"
},
books: {
collection: "books",
via: "library",
dominant: true
}
我不知道这里发生了什么。
delete library.books;
不起作用,因为关联不是模型对象中的字段。关联实际上存在于associations
对象中,读/写操作通过自定义getter/setter完成。你可以在waterline/model/lib/internalMethods/defineasassociations .js#L109:
Define.prototype.buildHasManyProperty = function(collection) {
var self = this;
// Attach to a non-enumerable property
this.proto.associations[collection] = new Association();
// Attach getter and setter to the model
Object.defineProperty(this.proto, collection, {
set: function(val) { self.proto.associations[collection]._setValue(val); },
get: function() { return self.proto.associations[collection]._getValue(); },
enumerable: true,
configurable: true
});
};
希望对你有帮助。
这会引起问题吗?这可以通过一开始就不填充关联来避免。执行model.toObject()
或model.toJSON()
,然后删除关联字段也应该工作。