Define a remote method on a LoopBack model

Bram Borggreve
InstructorBram Borggreve
Share this video with your friends

Social Share Links

Send Tweet
Published 8 years ago
Updated 6 years ago

In this lesson we look at extending the functionality of LoopBack models by defining a remote method.

A remote method is a method on a model exposed over a custom REST endpoint.

Using the lb remote-method command we will create the remote method meta-data in product.json. We will verify that this got created and that we see the new REST endpoint got added. Then we will create the actual method in product.js and enhance it so that the API will return an error if we want to buy a negative amount of products.

[00:00] We run lb remote-method and select product model. The name of our remote method will be buy. This is not a static method, so it will live on the prototype. The description will be, "Buy this product." The HTTP endpoint will be /buy, and the verb will be post. Then we hit enter, because we only want to add one endpoint.

[00:20] Next, the generator will ask us which arguments we want to accept. We enter quantity, which is of type number and is required. In this description, we will enter the number of products to buy.

[00:32] We will let LoopBack autodetect where to get the value from, and because we only need one argument, we will hit enter. Now, the generator asks us what we want the remote method to return. The name of our argument will be results, which is a type of object, and it will be the full response body.

[00:48] As a description, we will enter the results of the purchase. Now, we hit enter to finalize the generator. The generator tells us where it created the metadata and where we can define the actual remote method. It also gives us an example method we can use in our model.

[01:03] When we look at product.json in our IDE, we see that our buy method got defined here. When we open the API explorer and look in the product model, we see that our new endpoint got defined.

[01:13] When we enter the ID of a product and the quantity, and execute the remote method, we see that we get an error message. This is because we did not define the actual method, only the metadata.

[01:23] Let's open up product.js, and paste in the example code from the generator. When we execute the method again, instead of an error message, we now get no contents. This is because our result variable is empty. When we change it to an object literal and enter the quantity, we finally get a result.

[01:41] We can make the method a bit more useful by changing the result to an object with a status key and a friendly message. The result will now say, "You bought one product."

[01:50] Now, there's one thing that we need to fix here, and that's that we don't want to allow a quantity less than one. To do this, we first create a helper method in our model that checks for a valid quantity. This method will return false if the quantity is smaller than one.

[02:04] Now, we add a condition in our remote method that invokes the helper method. If the quantity is too low, we will return our callback with an error message. Let's try this out. We enter a quantity of -1, execute it, and an error message will be returned.

Victor Hazbun
Victor Hazbun
~ 7 years ago

how loopback knows when to return an http 500 error?

Rodolfo
Rodolfo
~ 7 years ago

Hello

When I define a remote method in model.json the id parameters is added, how I can avoid that? when I add the remote method in model.js that does not happen.

Markdown supported.
Become a member to join the discussionEnroll Today