dave-o
dave-o

Reputation: 47

StrongLoop loopback - how to exclude results without related model results

I'm using this Node API JSON, which returns Customers, their instances, and the instance versions.

Customers.find({
    "include": {
        "relation": "instances",
        "scope": {
            "include": {
                "relation": "versions"
            }
        }
    }
});

I would like to exclude all customers which do not have any related instances, in the result JSON, there is an "instances" entry with empty [ ]. however when I try to use this in a "where" I get a server error... any ideas, or am I going about this the wrong way?

Upvotes: 1

Views: 1326

Answers (1)

Terekhov
Terekhov

Reputation: 161

If you're using MongoDB as your database, then you could add a where property to your filter at the same level as your first include, like:

var filter = {
    where: {
        relationId: {
            exists: false
        }
    },
    include: {...}
};
Customers.find(filter, function ( err, results ) {...});

See issue #1009 for details/updates re: database implementation.

Alternatively, you could just filter the results with Lodash:

var customersWithInstances = _.filter( customers, function ( customer )
{
    return customer.instanceId;
});

Upvotes: 1

Related Questions