What does 'cb' mean in loopback?
Asked Answered
H

3

13

I am trying to learn loopback but I don't really understand what 'cb' means in function call. I read this In loopback documentation what does variable 'cb' stands for? and I have basic understanding of callback in nodejs but I just don't understand cb in loopback. For example, http://docs.strongloop.com/display/public/LB/Remote+methods.

module.exports = function(Person){

    Person.greet = function(msg, cb) {
      cb(null, 'Greetings... ' + msg);
    }

    Person.remoteMethod(
        'greet',  
        {
          accepts: {arg: 'msg', type: 'string'},
          returns: {arg: 'greeting', type: 'string'}
        }
    ); 
};

What does that cb mean? How can we know it accepts two parameters, null and a string? Hope someone could help.

House answered 6/7, 2015 at 7:25 Comment(3)
cb stands for callback, nothing special, just avariable name, can be replaced by any other variable nameCreek
it just a node style thing, in async, the first parameter of the callback genrally repesents the error object( that's the way to throw since it cannot be thrown synchronously) 2nd, 3rd ... nth parameters are the values you expect from the async method that you call...Creek
In this example, could you show me the async method because I'm not sure about thatHouse
C
12

So you have an Async function Person.greet which you'll call like this:

Person.greet('hello', function(err){
    ...
});

Notice that after 'hello' a second argument was passed and it is actually a function. It can also be defined outside with a name and passed this way:

function callback(err){
    ...
}
Person.greet('hello', callback);

Now it looks exactly how the Person.greet was defined:

Person.greet = function(msg, cb) {
  cb(null, 'Greetings... ' + msg);
}

The difference here is just that in the definition it uses a different name: cb. It could've used any name since to it cb is just an argument. But generally "cb", "done", or "next" are used as a standard practice.

Cuprite answered 6/7, 2015 at 8:30 Comment(8)
Well. I actually know what callback is. The main problem that confuses me is where they define the callback. Anyway, thanks.House
@KenKwok They don't define it, you do. You pass the function inline which they take as a named argument cb so that they can call itCuprite
@KenKwok what do you mean?Cuprite
In this case, when there is a POST request to /api/people/greet with data {"msg": "John"}, I could see "Greetings... John!" as response. I dont have to handle the callback but loopback seems to have this for me. That is why Im confused.House
@KenKwok I frankly don't fully understand what's confusing you, but this seems like an xy-problem to me. Maybe you should update your question with your actual code and change the title to what you actually want to understand. What cb means is independent of loopbackJS, and it's just a named function that another function takes as an argument so that it can call back later. It's short for "callback". Maybe you need to understand how callbacks work..Cuprite
Well. Perhaps what I don't understand is not the callback but how loopback works. Thank you bro.House
@KenKwok i know its late, but I think the returns object in loopback is defaulting the callbacks, that is why there is no custom definition in loopback and it is only applicable for APIsQuesnay
@Quesnay thanks bro. Actually I tried to write something with LoopBack and started to understand how the framework works but I think this comment would be useful for beginners of LoopBackHouse
S
4

Looking at the answers, it seems to me that only 1 of the two questions were ever answered.


Question 1: What does that cb mean?

This has been answered before that it is a short for the callback function.


Question 2: How can we know it accepts two parameters, null and a string?

You define this in the return option of your remote method which does the following according to the docs:

Describes the remote method's callback arguments; See Argument descriptions. The err argument is assumed; do not specify.

So if we look at your example

Person.remoteMethod(
    'greet',  
    {
      accepts: {arg: 'msg', type: 'string'},
      returns: {arg: 'greeting', type: 'string'}
    }
); 

You defined here that the callback parameters will be

callback(err, greeting: string)

Lets have another example from the docs:

    MyModel.remoteMethod('download', {
        isStatic: true,
        returns: [
            { arg: 'body', type: 'file', root: true },
            { arg: 'Content-Type', type: 'string', http: { target: 'header' } },
        ],
    });

For this example the callback will be

callback(err, body: file, Content-Type: string)

and the usage is like this

cb(null, stream, 'application/octet-stream');

Split answered 5/9, 2016 at 8:9 Comment(0)
B
1

I just came across the same question, and after a couple hours of frustration I've found the official answer.

https://docs.strongloop.com/display/public/LB/Remote+methods#Remotemethods-Howtodefinearemotemethod

OPTION ACCEPTS:

Defines arguments that the remote method accepts. These arguments map to the static method you define. For the example above, you can see the function signature: Person.greet(name, age, callback)... name is the first argument, age is the second argument and callback is automatically provided by LoopBack (do not specify it in your accepts array). For more info, see Argument descriptions. Default if not provided is the empty array, [].

Better answered 8/7, 2016 at 1:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.