'cb'在loopback中是什么意思?
What does 'cb' mean in loopback?
我正在尝试学习环回,但我不太明白 'cb' 在函数调用中的含义。我读了这个 并且我对 nodejs 中的回调有基本的了解,但我只是不了解环回中的 cb。
例如,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'}
}
);
};
那个cb是什么意思?我们怎么知道它接受两个参数,null 和一个字符串?希望有人能帮忙。
所以您有一个异步函数 Person.greet
,您可以这样调用它:
Person.greet('hello', function(err){
...
});
注意在 'hello'
之后传递了第二个参数,它实际上是一个函数。也可以在外面定义一个名字,这样传递:
function callback(err){
...
}
Person.greet('hello', callback);
现在看起来 Person.greet
是如何定义的:
Person.greet = function(msg, cb) {
cb(null, 'Greetings... ' + msg);
}
这里的区别只是在定义中使用了不同的名称:cb
。它可以使用任何名称,因为 cb
只是一个参数。但通常 "cb"、"done" 或 "next" 被用作标准做法。
我刚遇到同样的问题,经过几个小时的挫折,我找到了官方答案。
https://docs.strongloop.com/display/public/LB/Remote+methods#Remotemethods-Howtodefinearemotemethod
选项接受:
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, [].
看着答案,在我看来,这两个问题中只有一个得到了回答。
问题 1:那个 cb 是什么意思?
之前已经回答过,是回调函数的简称
问题2:我们怎么知道它接受两个参数,null和一个字符串?
您在远程方法的 return 选项中定义它,它根据 docs 执行以下操作:
Describes the remote method's callback arguments; See Argument descriptions. The err argument is assumed; do not specify.
所以如果我们看看你的例子
Person.remoteMethod(
'greet',
{
accepts: {arg: 'msg', type: 'string'},
returns: {arg: 'greeting', type: 'string'}
}
);
您在此处定义的回调参数将为
callback(err, greeting: string)
让我们再举一个来自文档的例子:
MyModel.remoteMethod('download', {
isStatic: true,
returns: [
{ arg: 'body', type: 'file', root: true },
{ arg: 'Content-Type', type: 'string', http: { target: 'header' } },
],
});
对于此示例,回调将是
callback(err, body: file, Content-Type: string)
用法是这样的
cb(null, stream, 'application/octet-stream');
我正在尝试学习环回,但我不太明白 'cb' 在函数调用中的含义。我读了这个
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'}
}
);
};
那个cb是什么意思?我们怎么知道它接受两个参数,null 和一个字符串?希望有人能帮忙。
所以您有一个异步函数 Person.greet
,您可以这样调用它:
Person.greet('hello', function(err){
...
});
注意在 'hello'
之后传递了第二个参数,它实际上是一个函数。也可以在外面定义一个名字,这样传递:
function callback(err){
...
}
Person.greet('hello', callback);
现在看起来 Person.greet
是如何定义的:
Person.greet = function(msg, cb) {
cb(null, 'Greetings... ' + msg);
}
这里的区别只是在定义中使用了不同的名称:cb
。它可以使用任何名称,因为 cb
只是一个参数。但通常 "cb"、"done" 或 "next" 被用作标准做法。
我刚遇到同样的问题,经过几个小时的挫折,我找到了官方答案。
https://docs.strongloop.com/display/public/LB/Remote+methods#Remotemethods-Howtodefinearemotemethod
选项接受:
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, [].
看着答案,在我看来,这两个问题中只有一个得到了回答。
问题 1:那个 cb 是什么意思?
之前已经回答过,是回调函数的简称
问题2:我们怎么知道它接受两个参数,null和一个字符串?
您在远程方法的 return 选项中定义它,它根据 docs 执行以下操作:
Describes the remote method's callback arguments; See Argument descriptions. The err argument is assumed; do not specify.
所以如果我们看看你的例子
Person.remoteMethod(
'greet',
{
accepts: {arg: 'msg', type: 'string'},
returns: {arg: 'greeting', type: 'string'}
}
);
您在此处定义的回调参数将为
callback(err, greeting: string)
让我们再举一个来自文档的例子:
MyModel.remoteMethod('download', {
isStatic: true,
returns: [
{ arg: 'body', type: 'file', root: true },
{ arg: 'Content-Type', type: 'string', http: { target: 'header' } },
],
});
对于此示例,回调将是
callback(err, body: file, Content-Type: string)
用法是这样的
cb(null, stream, 'application/octet-stream');