![](/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.