未处理的承诺拒绝nodejs
Unhandled promise rejection nodejs
我正在尝试使用 openweather-apis 连接到 dialogflow 代理。我是 promises 的新手,我不断收到警告 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,它从代理检索城市,调用调用函数,然后向用户发送响应。
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.
它不会解决您的问题,但一般来说,我会在 if(err) 之后添加“return”。因为否则将调用 resolve 。在您的特定情况下,它不会造成任何伤害,因为由于承诺的性质,它将被忽略。但是如果你在 reject 和 resolve 之间写了任何东西,它就会被执行。
// best practice
if (err) return reject(err)
对于你的问题,我刚刚尝试了这个快速测试来说服自己即使抛出也被 .catch() 捕获所以我认为你一定是 运行 一个 bad/old nodejs 版本,或者你提供的代码不完整,失败在elsewere。我在日志中没有看到任何指向您自己的代码的行 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)
});
确实发生了这个错误,因为这段代码无法处理 Promise Rejection。虽然我不确定哪个 Promise Rejection 未能处理,但基于 this and this GitHub 讨论。您似乎需要 return
agent.add()
函数。
我建议尝试 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
我正在尝试使用 openweather-apis 连接到 dialogflow 代理。我是 promises 的新手,我不断收到警告 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,它从代理检索城市,调用调用函数,然后向用户发送响应。
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.
它不会解决您的问题,但一般来说,我会在 if(err) 之后添加“return”。因为否则将调用 resolve 。在您的特定情况下,它不会造成任何伤害,因为由于承诺的性质,它将被忽略。但是如果你在 reject 和 resolve 之间写了任何东西,它就会被执行。
// best practice
if (err) return reject(err)
对于你的问题,我刚刚尝试了这个快速测试来说服自己即使抛出也被 .catch() 捕获所以我认为你一定是 运行 一个 bad/old nodejs 版本,或者你提供的代码不完整,失败在elsewere。我在日志中没有看到任何指向您自己的代码的行 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)
});
确实发生了这个错误,因为这段代码无法处理 Promise Rejection。虽然我不确定哪个 Promise Rejection 未能处理,但基于 this and this GitHub 讨论。您似乎需要 return
agent.add()
函数。
我建议尝试 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