简体   繁体   English

如何在Sails.js中检测对象是水线模型还是模型集合

[英]How do I detect if an object is a Waterline model or model collection in Sails.js

I need to traverse the tree of objects. 我需要遍历对象树。 Some objects are Waterline models or model collections, that I need to treat in a specific way. 有些对象是水线模型或模型集合,我需要以特定的方式对待它们。 So, how do I reliably detect if a JavaScript object is a Waterline model or collection in Sails.js? 因此,如何在Sails.js中可靠地检测JavaScript对象是Waterline模型还是集合?

here's a snippet. 这是一个片段。 in my model toJSON() call i need to know if i'm passing in a model or a json object. 在我的模型toJSON()调用中,我需要知道是否要传递模型或json对象。 so i just check for the associations property. 所以我只检查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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM