Search code examples
mysqlnode.jsrestloopbackjs

How to fix the callback was already called in loopback model


I have tried to extend api in loopback model. In the model, I used standard api of model such as findOne, create. The example codes are below

Subscriber.findOne({
 where : {
      email : "............."
 },
 function(err, instance){
      if(instance)
      {
           cb(null,instance);
           response = "success";
      }
 }
 cb(null, response);

But when I called this extended api, the error was occurred.

throw err:// Rethron non-MsSQL errors
    ^

Error: Callback was already called.

How to fix this error?


Solution

  • You need to use return inside the if callback as you haven't used else statement. Change your code to:

    Subscriber.findOne({
      where: {
        email: "............."
      },
      function(err, instance) {
        if (instance) {
          response = "success";
          return cb(null, instance);
        }
      },
      return cb(null, response);
    });