未处理的承诺拒绝 - 关键路径不完整
问题描述:
我一直从GCP收到关于此问题的错误信息,我正在使用在GAE上部署的数据存储库&。任何人有任何想法,为什么我使用JavaScript承诺这个错误?未处理的承诺拒绝 - 关键路径不完整
我正在使用google操作在谷歌主页上打开,如果设备尚未注册到数据存储中的公寓号,则需要激活关键字。如果它没有注册,它会要求输入一个关键词,它将把唯一的设备ID与一个公寓号码关联起来。如果唯一的ID有一个与之相关的公寓,那么会询问它可以提供什么帮助。
我不知道为什么它说关键路径不完整。 另外我是新承诺!因此,任何帮助是极大的赞赏
UnhandledPromiseRejectionWarning:未处理的承诺拒绝(拒绝编号:99):错误:关键路径元素一定不能是不完整的:激活:]
有了这个代码?
datastore.get(datastore.key([ACTIVATION, device_id]))
.then(results => {
let activation = null
if (results[0]) {
activation = results[0]
}
return Promise.resolve(activation)
})
.then(activation => {
console.log(activation)
let actionMap = new Map();
actionMap.set('input.welcome', assistant => {
console.log('input.welcome')
if (!activation) {
assistant.ask("Hello! May I have your key phrase?")
}
else {assistant.ask("Welcome back, what can I do for you today?")
}
})
actionMap.set('input.unknown', assistant => {
console.log('input.unknown')
if (!activation) {
assistant.ask("Please provide your activation code")
} else
{
let speech = "OK"
if (request.body &&
request.body.result &&
request.body.result.fulfillment &&
request.body.result.fulfillment.messages &&
request.body.result.fulfillment.messages[0] &&
request.body.result.fulfillment.messages[0].speech) {
speech = request.body.result.fulfillment.messages[0].speech
}
sendSMSFromUnit(activation.number, request.body.result.resolvedQuery)
assistant.tell("Got it. ")
}
})
actionMap.set('input.keyphrase', assistant => {
let activationCode = TitleCase([
assistant.getArgument('Token1'),
assistant.getArgument('Token2'),
assistant.getArgument('Token3')
].join(" "))
console.log('activationCode: ' + activationCode)
if (activation && activation.keyphrase == activationCode) {
assistant.tell('This device is activated.')
return
}
datastore.get(datastore.key([APARTMENT, activationCode]))
.then(results => {
console.log(results)
if (!results[0]) {
assistant.ask('Activation unsuccessful. Can you provide your activation code again?')
return
}
let apartment = results[0]
datastore.insert({
key: datastore.key([ACTIVATION, device_id]),
data: {
name: apartment.name,
number: apartment.number,
keyphrase: activationCode,
device_id: device_id
}
}).then(() => {
assistant.ask('Thanks! ')
})
})
})
答
承诺的整个格局是
Promise((resolve, reject) => {
// ...
});
现在如何使用它
promiseFunc(...)
.then((x) => {
// It get executed well
})
.catch((x) => {
// An error happened
});
在你的代码缺少.catch
部分。所以如果一个错误被抛入你的promise函数中,你将不会捕获它并导致节点异常。这就是为什么你有以下警告:Unhandled promise rejection
答
你会得到这个错误信息,因为你没有迎合当承诺拒绝而不是解决时 。
在你调用'.then'的代码中,那就是promise已经解决了。但是当承诺被拒绝时,你没有任何行动。以下面的例子;
// psuedo promise function which resolves if the data is good and rejects if the data is bad
function myPromiseFunction() {
return new Promise((resolve,reject) => {
// do something like make a http call here...
// if the response is good
return resolve(response)
// if the response is not so good
return reject(error)
});
}
// using the promise function
myPromiseFunction()
.then((response) => {
console.log(response);
}, (error) => { // <---- you are missing this part
console.log(error);
});
或者你可以把它写这样
myPromiseFunction()
.then((response) => {
console.log(response);
})
.catch((error) => { // <---- you are missing this part
console.log(error);
})