Promise 函数未解析 (javascript/Wit.ai)

Promise function not resolving (javascript/Wit.ai)

我正在尝试更新我的 messenger/wit.ai 聊天机器人中的功能,不再使用对承诺的回调。

这种原始格式执行得很好:

['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)
},

但是当我如下更新 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)
    })
},

我试过通过在整个函数中添加控制台日志来进行调试,如下所示:

函数触发时,中途停止,无法解决promise:

当我在函数中尝试 console.log(context) 时,我得到 'undefined'.

我错过了什么?

编辑:当我像这样删除函数参数周围的大括号时:

['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)
    })
},

我可以记录我的上下文,但仍然无法解决 Promise:

您的 buildSenario 应该看起来像这样,但是如果您以当前的方式使用它就没问题。您可以忽略它,因为它是一条警告消息。

buildScenario({{sessionId, context, text, entities}}) {});

您的某些回调或 Promise 似乎没有在 10 秒内得到解决。

我只在 wit.ai 上使用过 fb bot 集成,在 Messenger bot 节点中应该在接收短信时发送 200 状态。看看这段代码。

wit.ai example for fb messenger integration

原来我的包依赖项将我的 node-wit API 版本限制为 3.3.2,并且不允许它在以后更新(API 变成了基于 Promise 而不是在v4.0.0)。一旦我编辑了我的 package.json 文件以启用最新版本的 node-wit,我就开始工作了。