我正在尝试从API中提取数据,但是添加更新后却退出失败,原因是:\ "2020-01-01T00: 00: 00-0300 \"
没有此参数,则返回数组,但没有我想要的数据
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));
输出
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
答案 0 :(得分:1)
node:12444)UnhandledPromiseRejectionWarning:未处理的承诺 拒绝。该错误是由抛出异步内部引起的 没有捕获块或拒绝承诺 未使用.catch()处理。 上面的错误清楚地表明,当您抛出错误时,您应该处理承诺拒绝
状态码为400或以上时
Promise.reject('Oops!').catch(err => {
throw new Error(err);
})
被执行。上面的代码将拒绝应在承诺链中处理的承诺。因此,在promise链中添加如下所示的catch不会引发错误
.then(response => console.log(response.data)).catch((err)=>{
//handle error here
})
要更好地理解它,请检查此REPL https://repl.it/@sandeepp2016/HugeAvariciousLearning 首先执行它,然后删除注释,然后再次执行,您会看到区别
此外,您使用的这段代码有语法错误,请更正。
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}}}"})
我又创建了一个Repl签出。替换实际的API密钥,然后它应该可以工作了: