Search code examples
node.jsloopbackjs

Get items without related model


Hi i'm racking my brain out at this problem for some time and cant seem to find any solution without using custom function

I have two models

Inventory   --hasOne-->   Order
Order     --belongsTo-->  Inventory

And i want to list Inventory items without related Orders is there any way to do this with filter JSON?

Thanks for any advices :)


Solution

  • You may try to call this:

    http://localhost:3000/api/Inventory?filter={"where": {"orderId": null}}

    The disadvantage of this is that you have to make sure that the field is there and it's empty (value is set to null) if the property is not set you will have to build your own query.

    To pre-set the null value in the Inventory model, you can use "before save" hook and check if the orderId is set, then set it to null if not.

    Otherwise, you can just call a custom function, although I recommend using the default ones as long as possible.