Thursday, 1 June 2017

Node JS REST Api - relations between models

Lets say I have two routes: "cars" and "people" on my server.

app.get('/cars', getAll)
app.get('/cars/:id', get)
app.get('/people', getAll)

Everything works great. Now, I have to modify method "getAll" in "people" model. In this method I need to get cars which somebody owns by getting them by ID. I cannot use method "get" because in this method I have something like

res.status(200).send(car)

I need to return car, instead of http response. For now, I return promise so my routes look like below:

app.get('/cars/:id, (req, res) => {
   //...
   model.get(id).then(car => {
       res.status(200).send(car)
   })
})

but I am not sure if it is a good solution. The other way is to call API from method "getAll" in "people" model but it is even worse. What is best way to call methods from other models?

Regards



via Michal Bialek

No comments:

Post a Comment