简体   繁体   中英

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?

here's a snippet. in my model toJSON() call i need to know if i'm passing in a model or a json object. so i just check for the associations property.

    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;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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