为什么在此函数上未定义? [英] why am I getting undefined on this function?

查看:85
本文介绍了为什么在此函数上未定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个将Airtable用作数据库的对话流代理(库:airtable js)

I'm building a dialogflow agent that uses Airtable as database (library: airtable js)

一切正常,除非我无法获得 out of值。以便将其发送回dialogflow代理。

Everything works fine except I can't get the value "out of" the function in order to send it back to the dialogflow agent.

功能

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可以保证,但无法弄清楚如何使其正常工作。

I'm new to asynchronous programming, so I'm probably messing up with the Airtable js promises, but can't figure it out how to get it to work.

希望获得任何帮助

编辑

感谢@PRISONER的帮助。

THANKS @PRISONER FOR THE HELP.

出于需要,这是工作代码:

FOR THOSE IN NEED, HERE IS THE WORKING CODE:

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
              }
            }
          ]
        });
      });
  }


推荐答案

你是对的有关您如何使用Promises的一些问题。您在调用 firstPage()时使用了回调函数,而不是让它返回Promise。因此,您应该写成这样的部分:

You're correct, there are some issues with how you're using Promises. You're using a callback function in your call to firstPage() instead of having it return a Promise. So you could have written that part to look something like this:

  .firstPage()
  .then( records => {
    // Work with the records here
  })
  .catch( err => {
    // Deal with the error
  });

一旦您处理Promises,您想做的所有事情都必须在内部完成。 then()块。因此,您需要将 agent.add()移动到那里。

Once you're dealing with Promises, everything you want to do must be done inside the .then() block. So you'll need to move the agent.add() in there.

需要返回Promise,因此Dialogflow知道正在进行异步操作。由于 .then() .catch()函数返回Promise,因此您只需返回整个表情。所以类似

You also need to return the Promise, so Dialogflow knows that theres an asynchronous operation taking place. Since the .then() and .catch() functions return a Promise, you can just return the result of the whole expression. So something like

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

这篇关于为什么在此函数上未定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆