Notice: Undefined index: in /data/phpspider/phplib/misc.function.php on line 226
Javascript 未处理的承诺拒绝-密钥路径不完整_Javascript_Node.js_Google App Engine_Promise_Datastore - Fatal编程技术网

Javascript 未处理的承诺拒绝-密钥路径不完整

Javascript 未处理的承诺拒绝-密钥路径不完整,javascript,node.js,google-app-engine,promise,datastore,Javascript,Node.js,Google App Engine,Promise,Datastore,我一直从GCP那里得到一个关于这个的错误,我正在GAE上使用数据存储和部署。有人知道我为什么会在使用javascript时出现这个错误吗 我正在使用google操作在google home上打开,如果设备尚未注册到数据存储中的公寓号码,则要求输入激活关键字。如果未注册,它会要求提供一个关键字短语,该关键字短语将唯一设备id与公寓号码关联。如果唯一id有一个与之关联的单元,则is会询问它可以提供哪些帮助 我不知道为什么它说关键路径不完整。而且我是新的承诺!因此,我们非常感谢您的帮助 未处理的Pro

我一直从GCP那里得到一个关于这个的错误,我正在GAE上使用数据存储和部署。有人知道我为什么会在使用javascript时出现这个错误吗

我正在使用google操作在google home上打开,如果设备尚未注册到数据存储中的公寓号码,则要求输入激活关键字。如果未注册,它会要求提供一个关键字短语,该关键字短语将唯一设备id与公寓号码关联。如果唯一id有一个与之关联的单元,则is会询问它可以提供哪些帮助

我不知道为什么它说关键路径不完整。而且我是新的承诺!因此,我们非常感谢您的帮助

未处理的PromisejectionWarning:未处理的承诺拒绝(拒绝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! ')
            })
        })
    })

承诺的整个模式是

Promise((resolve, reject) => {
  // ...
});
现在如何使用它

promiseFunc(...)
   .then((x) => {
     // It get executed well
   })
   .catch((x) => {
     // An error happened
   });


代码中缺少
.catch
部分。所以,若一个错误被抛出到promise函数中,那个么您将无法捕获它,并导致节点异常。这就是为什么您会收到以下警告:
未处理的拒绝承诺

您会收到该错误消息,因为您没有满足以下要求: 这个承诺不是解决问题,而是拒绝

在您的代码中调用“.then”时,承诺已解决。但是当承诺被拒绝时,你没有行动。以下例,

// 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);
    });
//psuedo promise函数,用于解析数据是否正确,并在数据错误时拒绝
函数myPromiseFunction(){
返回新承诺((解决、拒绝)=>{
//在此处执行类似于进行http调用的操作。。。
//如果反应良好
返回解析(响应)
//如果反应不是很好
返回拒绝(错误)
});
}
//使用promise函数
我的承诺功能()
。然后((响应)=>{
控制台日志(响应);
},(错误)=>{//{
控制台日志(响应);
})
.catch((错误)=>{//
myPromiseFunction()
    .then((response) => {
        console.log(response);
    })
    .catch((error) => { // <---- you are missing this part
        console.log(error);
    })