shangsunset shangsunset - 4 months ago 9
Javascript Question

node.js - catch error thrown in a mongoose callback

In my controller, I have an end point function that calls another function, expecting to get a result or catch errors from the called function.

exports.createInvites = (req, res) => {
// code...

try {

generateInvitations(requirements, data => {

res.status(200).json({data});
});

} catch (err) {
console.log(`caught the error: ${err}`);

return res.status(500).json(err);
}
}


In the called function
generateInvites
, I save generated object to mongodb using mongoose, and throw an error if there is one.

function generateInvitations(requirements, cb) {

const { expirationDate, credits, numOfUse, numOfInvites } = requirements;
let invitationCodes = [];
for (let i = 0; i < numOfInvites; i++) {

const code = randomString(CODE_LENGTH);
invitationCodes.push(code);

const invitation = new Invitation({
// code,
numOfUse,
expirationDate,
credits
});

invitation.save((err, obj) => {

if (err) {
throw err; // it should throw a ValidationError here
}
});
}
cb(invitationCodes);

}


Above code is my attempt to handle errors thrown in
generateInvites
. Of course, I wasnt able to catch any error. What did I do wrong? and what should I do instead ?

Answer

You need to deal with the fact that the callback will fire long before an error would be thrown, as it's in a callback of it's own. I would suggest Promise.all to wait on all save operations, which return promises.

function generateInvitations(requirements) {
  const { expirationDate, credits, numOfUse, numOfInvites } = requirements;
  let promises = [];

  for (let i = 0; i < numOfInvites; i++) {
    const code = randomString(CODE_LENGTH);

    const invitation = new Invitation({
      // code,
      numOfUse,
      expirationDate,
      credits
    });

    promises.push(invitation.save());
  }
  return Promise.all(promises):
}

And then:

generateInvitations(requirements)
  .then(data => res.status(200).json({data}))
  .catch(err => {
    console.log(`caught the error: ${err}`);
    return res.status(500).json(err);
  });