本文介绍了什么是loopback中的回调(datasource。(automigrate))的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Loopback datasource API提供了,并带有可选回调。



我在一些例子中看到回调获取了一个参数(err),但没有定义。




  • 回调参数有什么形式?

  • 是否还有其他可能的参数?

  • 与其他功能如何?


解决方案



看看这个例子

pre> function printResult(err,result){
if(err){
console.log('something went wrong');
} else {
console.log(result);
}
}

function giveMeDouble(val,cb){
if(val!= 2){
var err = new Error不是2);
cb(err);
}
cb(null,2 * 2);
}
//将printResult函数作为回调函数传递给giveMeDoubleFunction
giveMeDouble(2,printResult);

其他方法

  giveMeDouble(2,function(err,result){
if(err){
console.log('something gone wrong');
} else {
console.log(result);
}
});

一般来说,在Loopback中的回调形式是第一个参数是err,第二个是成功res运行良好,但你可以总是有更多的参数取决于你调用的函数。
在你的case回调形式将是

  dataSource.automigrate(model,function(err,result){
})


Loopback datasource API offers automigrate function with an optional callback.

I see in some examples that the callback gets one parameter (err), but no definition of that.

  • What form does the callback parameter have?
  • Are there other possible parameters?
  • How is this with the other functions?

解决方案

Callbacks are nothing but the function which you passing as a parameter to the other function

Look at this example

function printResult(err,result) {
  if(err) {
    console.log('something went wrong');
  }else{
   console.log(result);
  }
}

function giveMeDouble(val, cb){
  if(val!=2){
    var err = new Error("value is not 2");
    cb(err);
  }
  cb(null,2*2);
}
// Passing printResult function as a callback to the giveMeDoubleFunction
giveMeDouble(2,printResult);

The Other Way of doing the same

giveMeDouble(2,function(err,result){
 if(err) {
   console.log('something went wrong');
  }else{
    console.log(result);
  }
});

Generally in Loopback form of callback is the first parameter is err and the second is the success res if everything went good but you can always have more parameters depend upon the function which you are calling.In your case callback form will be

dataSource.automigrate(model, function(err,result) {
})

这篇关于什么是loopback中的回调(datasource。(automigrate))的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 08:39