![](/img/trans.png)
[英]How to create 'calculated' fields at Waterline/Sails.js Model?
[英]How do I detect if an object is a Waterline model or model collection in Sails.js
我需要遍历对象树。 有些对象是水线模型或模型集合,我需要以特定的方式对待它们。 因此,如何在Sails.js中可靠地检测JavaScript对象是Waterline模型还是集合?
这是一个片段。 在我的模型toJSON()调用中,我需要知道是否要传递模型或json对象。 所以我只检查associations属性。
toJSON: function(users) {
var obj = this.toObject();
var arr = _.map(users, function(user) {
if (user.hasOwnProperty('associations')) {
return user.toJSON();
}
else {
return user;
}
});
obj.users = arr;
return obj;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.