Remote Method example
Hello, is it possible that you could add some meat to the Cars remote method? I'd like to see how to access instances of that model, return some basic calculation. Thank you.
What do you mean by "access instances of that model"? What are you trying to acheive?
By "access instances of that model" I mean that I want to make a remote method in the Cars model that finds instances of cars and does some calculation.
For example, I want to make an endpoint /avg-price-with-sound where user specifies an engine sound like "vrooom" and it the method returns the average price of all the cars in the datasource with sound "vrooom".
Car.avgPriceWithSound = function(s, cb) {
// pseudo code now
cars = Model.Cars.find({ "sound" : s });
for c in cars:
sum += c.price
avgPrice = sum / cars.length
cb(null,avgPrice);
// end of pseudo
};
Car.remoteMethod(
'avgPriceWithSound',
{
accepts: [{arg: 'sound', type: 'string'}],
returns: {arg: 'avgPrice', type: 'string'},
http: {path:'/avg-price-with-sound', verb: 'post'}
}
);
Perhaps you're looking for something like this? https://github.com/strongloop/loopback-getting-started-intermediate/blob/master/common/models/coffee-shop.js
Thanks, what I was looking for is how to find instances of Cars from within the remote method. I thought I had tried this before but for the record, its just Cars.find({'where': {'sound': 'vroom'}})
And to get the application context Cars.app or to find other models loopback.findModel(‘YourModel’)
Thank you for your help.