Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/457.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么我没有定义这个函数?_Javascript_Promise_Dialogflow Es_Dialogflow Es Fulfillment_Airtable - Fatal编程技术网

Javascript 为什么我没有定义这个函数?

Javascript 为什么我没有定义这个函数?,javascript,promise,dialogflow-es,dialogflow-es-fulfillment,airtable,Javascript,Promise,Dialogflow Es,Dialogflow Es Fulfillment,Airtable,我正在构建一个使用Airtable作为数据库的dialogflow代理(库:Airtable js) 一切正常,只是我无法从函数中获取值,以便将其发送回dialogflow代理 功能 function showSinglePrice(agent) { var finalPrice; var arraySinglePrice = null; const item = agent.context.get("item"), place = it

我正在构建一个使用Airtable作为数据库的dialogflow代理(库:Airtable js)

一切正常,只是我无法从函数中获取值,以便将其发送回dialogflow代理

功能

function showSinglePrice(agent) {
    var finalPrice;
    var arraySinglePrice = null;

    const item = agent.context.get("item"),
      place = item.parameters.place,
      size = item.parameters.size,
      type = item.parameters.type;

    base(tablePlaces)
      .select({
        maxRecords: 10,
        view: viewName,
        filterByFormula: `AND({type} = "${type}",{size} = "${size}",{place} = "${place}")` 
      })
      .firstPage(function(error, records) {
        if (error) {
          response.send({ error: error });
        } else {
          arraySinglePrice = records.map(record => {
            return {
              price: record.get("price")
            };
          });

          console.log(arraySinglePrice); //this works fine

          finalPrice = arraySinglePrice[0].price; //this works fine

          return finalPrice;
        }
      });   
   
    agent.add(`I wanted to get the result in here: ${finalPrice}`); //undefined
  }
我是异步编程新手,所以我可能弄乱了Airtable js的承诺,但不知道如何让它工作

非常感谢您的帮助

编辑

谢谢囚犯的帮助

对于需要帮助的人,以下是工作代码:

function showSinglePrice(agent) {    

    const item = agent.context.get("item"),
      place = item.parameters.place,
      size = item.parameters.size,
      type = item.parameters.type;

    return base(tablePlaces) //defined variable before this function
      .select({
        maxRecords: 1, //just want 1
        view: viewName, //defined variable before this function
        filterByFormula: `AND({type} = "${type}",{size} = "${size}",{place} = "${place}")`
      })
      .firstPage()
      .then(result => {
        
        console.log(result);

        var getPrice = result[0].fields.price;

        agent.add(`the current price is: $ ${getPrice}`); //its working
      })
      .catch(error => {
        console.log(error);

        response.json({
          fulfillmentMessages: [
            {
              text: {
                text: ["We got the following error..."] //will work on it
              }
            }
          ]
        });
      });
  }

你是对的,你如何使用承诺存在一些问题。您在调用
firstPage()
时使用了回调函数,而不是让它返回承诺。所以你可以把这部分写成这样:

  .firstPage()
  .then( records => {
    // Work with the records here
  })
  .catch( err => {
    // Deal with the error
  });
处理承诺后,您想做的一切都必须在
.then()块中完成。因此,您需要在其中移动
agent.add()

您还需要返回承诺,以便Dialogflow知道发生了异步操作。由于
.then()
.catch()
函数返回一个承诺,因此您可以只返回整个表达式的结果。大概是

  return base(tablePlaces)
      .select(query)
      .firstPage()
      .then(/*function*/)
      .catch(/*function*/);

agent.add()
移到
firstPage
回调中,并用它替换
return
return finalPrice
无处可返回,在所有选择链完成之前,您正在调用add()。如果我这样做,我会得到以下错误:UnhandledPromiseRejectionWarning:错误:没有为平台定义响应:在V2Agent.sendResponses处为null(/rbd/pnpm volume/54a37ce2-dee9-4e33-af7f-21d280ce7234/node\u modules/.registry.npmjs.org/dialogflow fulfillment/0.6.1/node\u modules/dialogflow fulfillment/src/v2 agent.js:243:13)未处理的PromisejectionWarning:未处理的承诺拒绝。此错误源于在没有catch块的情况下抛出异步函数的内部,或者拒绝未使用.catch()处理的承诺。您是否厌倦了拯救他人的生命我为此挣扎了3天。。tks!我很高兴这有帮助!我只是尽我所能去帮助别人,回报那些帮助过我(现在仍然帮助着我!)的人。