1

I have written one recursive query to update data in database level wise or hierarchical manner.

I have written this code :

let updateInvitationRewards = function(parent, level, callback) {

  Service.customer.getOneCustomer({
    _id: parent
  }, {}, {}, function(err, data) {

    console.log(data.email);
    level--;
    if (data.parent) {
      parent = data.parent.id;
    } else if (level == 0 || data.parent == null)
      callback("end");

    updateInvitationRewards(parent, level--, callback);
  });

};

updateInvitationRewards(parent, level, function(string) {
  console.log(string);
  cb();
});

This query is giving error like this :

Error: Callback was already called.

Please help me I am new to these type of callbacks.

3
  • Can you try inserting updateInvitationRewards(parent, level--, callback); inside the if(data.parent) condition? Commented Jun 29, 2016 at 13:33
  • I have not removed insertion code from this portion of code for data clarity...but this much of code is giving error... Callback was already called..I am not getting it .. Commented Jun 29, 2016 at 13:36
  • The error comes when you call callback function more than once. In your code, when the level becomes 0 or when parent is null, the callback is called. But your code will continue to call the udpateInvitationRewards even when the else if condition is satisfied. So by the next time the parent is null, callback will be called for the second time. This could throw error. Commented Jun 29, 2016 at 13:46

2 Answers 2

2

With all brackets, we have :

if (data.parent) {
  parent = data.parent.id;
} else if (level == 0 || data.parent == null) {
  callback("end");
}

updateInvitationRewards(parent, level--, callback);

if level equals 0 or data.parent is null, the callback is trigger, then updateInvitationRewards with the same callback.

To fix it :

if (data.parent) {
  parent = data.parent.id;
} else if (level == 0 || data.parent == null) {
  return callback("end"); // the execution stops here
}

updateInvitationRewards(parent, level--, callback);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank u very much :)
1

On any function the Callback should be called only once. If the callback is called more than once the Error: Callback was already called. would come.

From your code sample, it could be seen that the function will be recursively called. So the error could appear when the level becomes 0 or when the parent is null.

Consider the following scenario, when the level is null or the parent is null for the first time the callback will be called. But the recursive call will continue to happen. Suppose in the next execution, if the parent is null the callback will be called for the second time and this throws the error.

From the code, I couldn't see what is the use of level, but I would suggest the following code to be implemented, which makes sure that the callback isn't called for the next time.

let updateInvitationRewards = function(parent, level, callback) {

    Service.customer.getOneCustomer({
        _id: parent
    }, {}, {}, function(err, data) {
        console.log(data.email);
        level--;
        if (data.parent) {
            parent = data.parent.id;
            updateInvitationRewards(parent, level--, callback);
        } else if (level == 0 || data.parent == null)
            callback("end");
    });
};

updateInvitationRewards(parent, level, function(string) {
    console.log(string);
    cb();
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.