我不断收到GCP的错误,我正在使用数据存储区&在GAE上部署。任何人有任何想法为什么我使用JavaScript承诺收到此错误?
我正在谷歌主页上使用谷歌操作,如果设备尚未注册到数据存储区中的公寓号,请询问激活密钥短语。如果未注册,则会要求提供将唯一设备ID与公寓号相关联的关键短语。如果唯一ID具有与之关联的公寓,则询问它可以提供哪些帮助。
我不确定为什么说关键路径不完整。 我也是承诺的新手!所以非常感谢任何帮助
UnhandledPromiseRejectionWarning:未处理的承诺拒绝(拒绝ID: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! ')
})
})
})
答案 0 :(得分:1)
承诺的整个模式是
Promise((resolve, reject) => {
// ...
});
现在该如何使用
promiseFunc(...)
.then((x) => {
// It get executed well
})
.catch((x) => {
// An error happened
});
在您的代码中,您缺少.catch
部分。因此,如果错误被抛入您的promise函数,您将无法捕获它并导致节点异常。这就是您收到以下警告的原因:Unhandled promise rejection
答案 1 :(得分:0)
您收到该错误消息是因为您不适合何时使用 承诺拒绝而不是解决。
在您调用'.then'的代码中,即承诺已解决的时间。但是,当承诺被拒绝时,你没有任何行动。以下面的例子为例;
HAVING
或者你可以这样写它
// 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);
});