[英]Promise function not resolving (javascript/Wit.ai)
I'm trying to update the functions in my messenger/wit.ai chat bot from using callbacks to promises. 我正在尝试从使用回调到Promise更新我的Messenger / wit.ai聊天机器人中的功能。
This original format executes fine: 这种原始格式可以很好地执行:
['buildScenario'](sessionId, context, cb) {
var trendChoice = scenarioCombos['trends']
var disruptionChoice = scenarioCombos['disruptions']
context.trend = trendChoice[Math.floor(Math.random() * trendChoice.length)]
context.disruption = disruptionChoice[Math.floor(Math.random() * disruptionChoice.length)]
cb(context)
},
But when I update to Promises as below, it doesn't make it through: 但是当我如下更新到Promises时,它并没有通过:
['buildScenario']({sessionId, context, entities}) {
return new Promise(function(resolve, reject) {
var trendChoice = scenarioCombos['trends']
var disruptionChoice = scenarioCombos['disruptions']
context.trend = trendChoice[Math.floor(Math.random() * trendChoice.length)]
context.disruption = disruptionChoice[Math.floor(Math.random() * disruptionChoice.length)]
return resolve(context)
})
},
I've tried debugging by adding console logs throughout the function like this: 我尝试通过在整个函数中添加控制台日志来进行调试,如下所示:
When the function is triggered, it stops halfway through and fails to resolve the promise: 触发该函数时,它会中途停止,并且无法兑现承诺:
When I try console.log(context) within the function I get 'undefined'. 当我在函数中尝试console.log(context)时,得到“未定义”。
What am I missing? 我想念什么?
EDIT: When I remove the curly brackets around my function parameters like so: 编辑:当我像这样删除函数参数周围的花括号时:
['buildScenario'](sessionId, context, entities) {
console.log('BS POINT 1')
return new Promise(function(resolve, reject) {
console.log('BS POINT 2')
var trendChoice = scenarioCombos['trends']
console.log(trendChoice)
console.log('BS POINT 3')
var disruptionChoice = scenarioCombos['disruptions']
console.log(disruptionChoice)
console.log('BS POINT 4')
console.log(context)
context.trend = trendChoice[Math.floor(Math.random() * trendChoice.length)]
console.log(context)
console.log('BS POINT 5')
context.disruption = disruptionChoice[Math.floor(Math.random() * disruptionChoice.length)]
console.log(context)
console.log('BS POINT 6')
return resolve(context)
})
},
I'm able to log my context but still can't resolve the Promise: 我可以记录我的上下文,但是仍然无法解决Promise:
Your buildSenario should look like this, but its fine if you use it in your current way. 您的buildSenario应该看起来像这样,但是如果您以当前方式使用它,那就很好。 You can ignore it coz its a warning message.
您可以忽略它,因为它是警告消息。
buildScenario({{sessionId, context, text, entities}}) {});
It seems that your some callback or Promises not geting resolved within 10 second. 您的某些回调或Promises似乎在10秒内未解决。
I have only worked on wit.ai with fb bot integartion, in messenger bot node should send 200 status on receiving text message. 我只在wit.ai和fb bot integartion上工作过,在Messenger bot节点中,接收到短信时应发送200状态。 Have a look at this code.
看一下这段代码。
wit.ai example for fb messenger integration FB Messenger集成的wit.ai示例
Turns out my package dependencies were limiting my node-wit API version to 3.3.2 and not allowing it to update any later (the API became Promise-based rather than using callbacks in v4.0.0). 事实证明,我的程序包依赖关系将我的node-wit API版本限制为3.3.2,并且不允许以后进行任何更新(API成为基于Promise的版本,而不是在v4.0.0中使用回调)。 Once I edited my package.json file to enable the latest version of node-wit I got it working.
一旦我编辑了package.json文件以启用最新版本的node-wit,我就开始使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.