[英]Unhandled promise rejection nodejs
我正在嘗試使用openweather-apis連接到對話流代理。 我是新的承諾,我不斷收到警告UnhandledPromiseRejectionWarning
,我不確定如何解決這個問題。
目前我有 2 個文件 weather.js,這使得 api 調用
const api = require("openweather-apis")
api.setAPPID(process.env.API_KEY)
api.setUnits("metric")
module.exports = {
setCity: function(city) {
api.setCity(city)
},
getWeather: function() {
return new Promise(function(resolve, reject) {
api.getTemperature(function(err, temp) {
if (err) reject(err)
resolve(temp)
})
})
}
}
我使用 weatherinCity.js,它從代理中檢索城市,調用調用 function,然后向用戶發送響應。
const weather = require("../../weather")
module.exports = {
fulfillment: function(agent) {
const city = agent.parameters.geo_city
weather.setCity(city)
weather.getWeather().then(function(temp) {
agent.add(
"It is ${temp} degrees Celcius in ${city}"
)
}).catch(() => {
console.error("Something went wrong")
})
}
}
完整的錯誤信息:
(node:2896) UnhandledPromiseRejectionWarning: Error: No responses defined for platform: DIALOGFLOW_CONSOLE
at V2Agent.sendResponses_ (C:\Users\Coen\Desktop\ciphix-ca-case\node_modules\dialogflow-fulfillment\src\v2-agent.js:243:13)
at WebhookClient.send_ (C:\Users\Coen\Desktop\ciphix-ca-case\node_modules\dialogflow-fulfillment\src\dialogflow-fulfillment.js:505:17)
at C:\Users\Coen\Desktop\ciphix-ca-case\node_modules\dialogflow-fulfillment\src\dialogflow-fulfillment.js:316:38
at processTicksAndRejections (internal/process/task_queues.js:93:5)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:2896) 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: 1)
(node:2896) [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.
確實發生了此錯誤,因為此代碼無法處理 Promise 拒絕。 雖然我不確定哪個 Promise Rejection 未能處理,但基於此和此GitHub 討論。 看來您需要return
agent.add()
function。
我建議嘗試async-await
風格,結果是你必須添加一個try catch block
module.exports = {
fulfillment: async function(agent) {
try {
const city = agent.parameters.geo_city
weather.setCity(city)
let temp = await weather.getWeather()
agent.add(
"It is ${temp} degrees Celcius in ${city}"
)
} catch (err) {
console.error("Something went wrong")
console.error(err)
}
}
}
在try block
上拋出的每個錯誤都應該在catch block
中被捕獲。 不要忘記在function
之前添加async
。
它不會解決你的問題,但一般來說,我會在 if(err) 之后添加“return”。 因為否則將完成對解決的調用。 在您的特定情況下,它不會造成任何傷害,因為由於承諾的性質,它將被忽略。 但是如果你在拒絕和解決之間寫了任何東西,它就會被執行。
// best practice
if (err) return reject(err)
對於您的問題,我剛剛嘗試過這個快速測試來說服自己,即使是 throws 也會被.catch() 捕獲,所以我認為您必須運行一個壞/舊的 nodejs 版本,或者您提供的代碼不完整,並且失敗是別的。 我在日志 O_o(僅 node_modules)中沒有看到任何指向您自己的代碼的行。
它是哪個nodejs版本?
var p = new Promise((resolve, reject) => {
throw new Error('test');
resolve('ok')
})
p.then(console.log).catch(function(err) {
console.error('err', err)
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.