Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does 'cb' mean in loopback?

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.

like image 792
Ken Kwok Avatar asked Jul 06 '15 07:07

Ken Kwok


3 Answers

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.

like image 87
laggingreflex Avatar answered Nov 15 '22 18:11

laggingreflex


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');

like image 3
julianalimin Avatar answered Nov 15 '22 17:11

julianalimin


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, [].

like image 1
Roberto Aguilar Avatar answered Nov 15 '22 17:11

Roberto Aguilar