javascript中的方法承诺不在对话框流上运行

问题描述 投票:2回答:1

所以这是在dialogflow v2中使用的yelp-fusion node.js API的代码。

问题:agent.add(response.jsonBody.businesses[0].name);应该让机器人说即使代码存在,业务的名称也不会实际运行。

从研究中,其他答案提到需要在这个javascript承诺中使用fat arrow =>。

但是,它已被使用。 .then()中的代码没有运行,但运行时的console.log除外。

任何人都可以建议我可以做些什么来运行javascript承诺内的方法?还是其他选择?非常感激。谢谢!

下面的客户端是yelp API客户端。

agent是对话框流中的webhookclient。 agent.add()在以下代码之外执行时有效。

    client.search({
      term:'Four Barrel Coffee',
      location: 'san francisco, ca'
    }).then(response => {
      //res = response.jsonBody.businesses[0].name; //*not assigned!
      console.log(response.jsonBody.businesses[0].name); 
      agent.add(response.jsonBody.businesses[0].name); //*nothing!
    }).catch(e => {
      console.log(e);
    });
javascript node.js dialogflow yelp yelp-fusion-api
1个回答
1
投票

你有一半的解决方案。使用fat-arrow并不是那么多,而是你正在处理异步函数(client.search调用),当你使用对话框实现库的异步函数时,你需要使用Promises。

具体来说 - 你需要返回一个Promise,所以调用函数知道它必须等待所有then()子句完成才能发送回复。

您没有显示整个函数,但您可以通过添加一些return语句来实现。可能是这样的:

return client.search({
  term:'Four Barrel Coffee',
  location: 'san francisco, ca'
}).then(response => {
  return agent.add(response.jsonBody.businesses[0].name);
}).catch(e => {
  console.log(e);
  return Promise.reject( e );
});
© www.soinside.com 2019 - 2024. All rights reserved.