Rocky
Rocky

Reputation: 431

how i can access api of one model into another

enter image description hereHello guys i m new to loopback. I go through all documentation as well as Google but haven't found any solution to my question.

My question is that, if we can access one model into another for eg consider 2 model product and shop, if this both model does not have relation than we can export one model into another

eg:- code

module.exports = function(product ) {
 var Shop=app.models.shop;
};

Now main thing is that if we can call one model into another (as show above) than is it, mean we can call all rest api related to that model?

Why i want this thing suppose user fill all product as well as shop info through a single form (font end) then model of either shop or product is call. Suppose i call model shop in this model i have to create a remote method to save product related info this thing i know so far.

So how i can use rest end point api of product to save data is this is not possible?

for eg:-

  1. If we want to save shop detail then we call rest api like this (get) api/shops

  2. If we want to save product detail then we call rest api like this (get) api/products

Now how i can call point 2 api into shop model is it possible then how if not, then why?

Upvotes: 0

Views: 471

Answers (1)

Raymond Camden
Raymond Camden

Reputation: 10857

I'm having a hard time understanding exactly what you are asking here, but I'll take a stab. First off - you can call any API from your model code. It's just a HTTP end point so you can use a library like request() to call it.

However, you generally should not bother with a HTTP call to another LoopBack model when LoopBack provides a server-side API that does the same things the REST api does. So for ex, if you need to get all of one model type, or one object, or search, etc, that can all be done via local calls.

I'd start with the "Working with Data" docs: http://loopback.io/doc/en/lb3/Working-with-data.html. It shows both REST and local Node API versions of working with your data.

Upvotes: 1

Related Questions