Luis Henrique
Luis Henrique

Reputation: 771

UnhandledPromiseRejectionWarning: Unhandled promise rejection - API

I'm trying to extract data from an API, but I'm getting an unsuccessful exit when I add updatedSince: \ "2020-01-01T00: 00: 00-0300 \"

Without this parameter the array is returned, but it doesn't have the data I want

index.js

require('es6-promise').polyfill();
require('isomorphic-fetch');

fetch('https://www.bluesight.io/graphql', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json',
               'Bluesight-API-Token': 'api-token-here'},
    body: JSON.stringify({"query": "{squads {name cards(includedOnKanban: true, closed: false, archived: false, cancelled: false, updatedSince: \"2020-01-01T00:00:00-0300\") {identifier title description status priority assignees { fullname email } secondaryLabel primaryLabels}}}"}),})           

.then(function(response) {

    if (response.status >= 400) {

        return Promise.reject('Oops!').catch(err => {
            throw new Error(err);
          });

    }else{

        console.log("Method: POST\nContent-Type: application/json\nBluesight-API-Token: OK\nQuery: OK");
        response.json();

    }

    return response.text();
})

.then(response => console.log(response.data));

OUTPUT

PS C:\Users\Documents\Bluesight> node .\index.js
(node:12444) UnhandledPromiseRejectionWarning: Error: Oops!
    at C:\Users\Documents\Bluesight\index.js:15:19
(node:12444) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 3)
(node:12444) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
PS C:\Users\Documents\Bluesight> node .\index.js
(node:20236) UnhandledPromiseRejectionWarning: Error: Oops!
    at C:\Users\Documents\Bluesight\index.js:15:19
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:20236) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 3)
(node:20236) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

POSTMAN

enter image description here

Upvotes: 0

Views: 505

Answers (1)

Sandeep Patel
Sandeep Patel

Reputation: 5148

node:12444) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). Above error, clearly says that you should handle promise rejection as you are throwing error

When status code is 400 or more than this code is

Promise.reject('Oops!').catch(err => {
            throw new Error(err);
          })

being executed. The above code will reject promise that should be handled in a promise chain. So adding catch as shown below in promise chain will not throw the error

.then(response => console.log(response.data)).catch((err)=>{
   //handle error here
 })

To understand it better check this REPL https://repl.it/@sandeepp2016/HugeAvariciousLearning Execute it first and then remove the comment and execute again, you would see the difference

Also, this code you are using has a syntax error so correct it.

JSON.stringify({"query": "{squads {name cards(includedOnKanban: true, closed: false, archived: false, cancelled: false, updatedSince: \"2020-01-01T00:00:00-0300\") {identifier title description status priority assignees { fullname email } secondaryLabel primaryLabels}}}"})   

I have created one more Repl check that out.Replace actual API key and then it should work:

https://repl.it/@sandeepp2016/TrainedFinancialLaboratory

Upvotes: 1

Related Questions