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.
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.
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');
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 youraccepts
array). For more info, see Argument descriptions. Default if not provided is the empty array, [].
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With