2

I would like to know how to send error code in promise as response in nodejs. Error in Promise Not sending any error in response object in NodeJS/Express

module.exports.providerData = function (reqUrl, query) {
  return new Promise(async function (resolve, reject) {
    try {
      var validUrl = checkValidParamters(reqUrl, query);
      if(validUrl != true){
       throw new Error('failed');
      }
      else{
        sendcountry = countryjson.filter(function (send) {
          return send.country_name == param_sendcountry;
        })
        var provider_sendcncode = sendcountry[0].country_code;
        var receive = countryjson.filter(function (pr) {
          return pr.country_name == param_receivecountry;
        })
        var valid = true;
        var valid_obj = { validstatus: valid};
        resolve(valid_obj);
      }
    }
    catch (err) {
     reject(err);
    }
  });

}

in app.js

router.get('/', function (req, res, next) {
  if (getcountries == null && getcurrencies == null && getproviders == null) {
            util.providerData(req.originalUrl, req.query).then(obj => {
              res.render("corridor.ejs");
            }).catch(err=>{
              res.status(401).send({ error : err.message });
            })
  }
  else {
    console.log('just return');
          util.providerData(req.originalUrl, req.query).then(obj => {
              res.render("corridor.ejs");
            }).catch(err=>{
              res.status(401).send({ error : err.message });
            })
  }
});

I need to catch the error response res.status(401).send({ error : err.message }); if export function is returning false/error

8
  • As far as I can see, the code seems alright and should work. Which part is failing? Commented Mar 25, 2019 at 3:35
  • @varunagarwal if the export function return validUrl as false, i will catch the error as "Failed", in app.js it should res.status(401).send({ error : err.message }) , failing to send response status Commented Mar 25, 2019 at 3:51
  • Basically your client is not receiving the 401 error status on making the api call? Commented Mar 25, 2019 at 3:54
  • @varunagarwal yes Commented Mar 25, 2019 at 4:01
  • post checkValidParamters() code , also use debugger to find the issue or just console.log(validUrl) to check what actually you are getting. Commented Mar 25, 2019 at 7:09

1 Answer 1

2

your promise doesn't resolve anything. to resolve, you just simple return your response.

router.get('/', function (req, res, next) {
   if (getcountries == null && getcurrencies == null && getproviders == null) {
        return util.providerData(req.originalUrl, req.query).then(obj => {
          return res.render("corridor.ejs");
        }).catch(err=>{
          return res.status(401).send({ error : err.message });
        })
  }
    else {
       console.log('just return');
       return util.providerData(req.originalUrl, req.query).then(obj => {
          return res.render("corridor.ejs");
        }).catch(err=>{
          return res.status(401).send({ error : err.message });
        })
  }
});
Sign up to request clarification or add additional context in comments.

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.