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.

Community
  • 1
  • 1
Ken Kwok
  • 388
  • 3
  • 19
  • 1
    `cb` stands for callback, nothing special, just avariable name, can be replaced by any other variable name – mido Jul 06 '15 at 07:34
  • 1
    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... – mido Jul 06 '15 at 07:35
  • In this example, could you show me the async method because I'm not sure about that – Ken Kwok Jul 06 '15 at 07:40

3 Answers3

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.

laggingreflex
  • 32,948
  • 35
  • 141
  • 196
  • Well. I actually know what callback is. The main problem that confuses me is where they define the callback. Anyway, thanks. – Ken Kwok Jul 07 '15 at 01:15
  • 1
    @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 it – laggingreflex Jul 07 '15 at 09:09
  • @KenKwok what do you mean? – laggingreflex Jul 07 '15 at 15:22
  • 1
    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. – Ken Kwok Jul 07 '15 at 15:29
  • @KenKwok I frankly don't fully understand what's confusing you, but this seems like an [xy-problem](http://meta.stackexchange.com/questions/66377/what-is-the-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](http://google.com/search?q=how+callbacks+works+%28javascript%29).. – laggingreflex Jul 07 '15 at 15:42
  • Well. Perhaps what I don't understand is not the callback but how loopback works. Thank you bro. – Ken Kwok Jul 08 '15 at 02:22
  • 2
    @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 APIs – anoop Mar 17 '16 at 02:18
  • @anoop 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 LoopBack – Ken Kwok Mar 22 '16 at 05:23
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');

julianalimin
  • 153
  • 2
  • 11
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, [].

Roberto Aguilar
  • 457
  • 5
  • 6