最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - node.js - catch error thrown in a mongoose callback - Stack Overflow

programmeradmin1浏览0评论

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 ?

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 ?

Share Improve this question asked Aug 5, 2016 at 17:23 shangsunsetshangsunset 1,6154 gold badges23 silver badges38 bronze badges 1
  • 1 you should look into promises. async error handling is ugly with callbacks. promises make it much more.. civilized – thedarklord47 Commented Aug 5, 2016 at 17:30
Add a ment  | 

1 Answer 1

Reset to default 1

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);
  });
发布评论

评论列表(0)

  1. 暂无评论