Skip to content Skip to sidebar Skip to footer

What Is The Callback In Loopback (datasource.(automigrate))

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 f

Solution 1:

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

Look at this example

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

functiongiveMeDouble(val, cb){
  if(val!=2){
    var err = newError("value is not 2");
    cb(err);
  }
  cb(null,2*2);
}         
// Passing printResult function as a callback to the giveMeDoubleFunctiongiveMeDouble(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) {
})

Post a Comment for "What Is The Callback In Loopback (datasource.(automigrate))"