2

I am trying to make a call to an API to grab some data. When the call returns valid data, it works! However, when it hits a API error, or an error I want to create based off the data response, I get this error:

Unhandled rejection Error: Data not found!
at Request.request.post [as _callback]
.
.

These are the files I am using:

let grabSomeData = new BluebirdPromise((resolve, reject) => {

  pullers.grabData(dataID, (err, res) => {

    if (err) {
      return reject(err);
    }

    return resolve(res);
  });


});

grabSomeData.then((fulfilled, rejected) => {
  console.log('res: ' + fulfilled);
  console.log('rej: ' + rejected);
});

In my other file making the http request,

grabData(dataID, grabDataCallback)  {

  let bodyObj = {
    query: dataByIDQuery,
    variables: {
      id: dataID
    }
  };

  // grab the data
  request.post(
    {
      url: dataURL,
      body: JSON.stringify(bodyObj)
    }, (err, httpResponse, body) => {

        if (err) {
          return grabDataCallback(err);
        }

        let res = JSON.parse(body);

        if (res.data.dataByID !== null) {
          return grabDataCallback(null, res.data.dataByID);
        }

        return grabDataCallback(Boom.notFound('Data not found!'));
      }
  );

}
parwatcodes
  • 6,669
  • 5
  • 27
  • 39
  • what does `console.log(res.data.dataByID)` gives you – parwatcodes Mar 09 '17 at 16:34
  • When purposefully testing for an error, it will not reach this far. It will display the error above. When testing for valid data, it will print the data just fine @p0k8_ – nicolascoding Mar 09 '17 at 16:36
  • use the last return statement in the else block `if(res.data.dataByID !== null) { return grabDataCallback(null, res.data.dataByID); } else { return grabDataCallback(Boom.notFound('Data not found!')); }` – parwatcodes Mar 09 '17 at 16:38
  • @p0k8_ Just tried this, I am getting the same error. I believe it has to do with the way I am handling the reject/resolve using a callback. – nicolascoding Mar 09 '17 at 16:40
  • yes, you haven't declared the `catch` block to handle the reject – parwatcodes Mar 09 '17 at 16:43

1 Answers1

2

Instead of this:

grabSomeData.then((fulfilled, rejected) => {
  console.log('res: ' + fulfilled);
  console.log('rej: ' + rejected);
});

You need to use:

grabSomeData.then((fulfilled) => {
  console.log('res: ' + fulfilled);
}, (rejected) => {
  console.log('rej: ' + rejected);
});

Or:

grabSomeData.then((fulfilled) => {
  console.log('res: ' + fulfilled);
}).catch((rejected) => {
  console.log('rej: ' + rejected);
});

For more info on the unhandled rejection warnings (that will be fatal errors in the future) see this answer:

Community
  • 1
  • 1
rsp
  • 107,747
  • 29
  • 201
  • 177
  • Ok Awesome!, now would this same principle work with Promise.all (because I am pulling multiple things asynchronously at once? – nicolascoding Mar 09 '17 at 16:46
  • @nicolascoding Yes. When you run Promise.all(...) then you either have to give to functions as arguments to then like `Promise.all(...).then((val) => console.log('value:', val), (err) => console.log(err));` or use then with one function and catch with the second one like this `Promise.all(...).then((val) => console.log('value:', val)).catch((err) => console.log(err));` but your `val` will be an array. – rsp Mar 09 '17 at 16:49