【问题标题】:Cheerio.load messes with responses for Google AssistantCheerio.load 与 Google Assistant 的响应相混淆
【发布时间】:2018-12-02 05:59:13
【问题描述】:

我有这个意图,它调用了cheerio.load() 并且它与响应混淆。谷歌助理一直告诉我没有设置响应,即使稍后在代码中我有响应。控制台还告诉我没有将异步调用返回给处理程序,我相信这是cheerio.load()。无论如何我可以解决这个问题,以便它继续在代码底部寻找正确的 conv.ask 吗?它仍然继续运行到那里,因为 console.log(map) 出现了。感谢您的帮助!

app.intent("late drop", (conv,{term,year}) => {
var date = new Date();
var month;
if(term == null){
    month = date.getMonth();

    if(month >= 9 && month <=12){
        term = "fall";
        //console.log("fall")
    }
    else if (month >= 1 && month <= 5) {
        term = "spring";
        //console.log("spring")
    }
    else {
        term = "summer";
        //console.log("summer")
    }
}
if(year == null){
    yearDig = date.getFullYear();
    year = yearDig;
    //console.log(year)
}
var strYear = year.toString();
var semester = term+strYear.substr(2);
const options = {
    uri: `https://www.registrar.psu.edu/academic_calendar/${semester}.cfm`,
    transform: function (body) {
        return cheerio.load(body);
  }
};
rp(options)
.then(($) => {
    let map = {};
    let columnOne = [];
    let columnThree = [];

    $('table').find('tr td:nth-child(1)').each(function (index, element) {
        columnOne.push($(element).text());
      });

    $('table').find('tr td:nth-child(3)').each(function (index, element) {
        columnThree.push($(element).text());
    });

    columnOne.forEach((item, i) => {
        map[item] = columnThree[i];
    });

    console.log(map);
    date = map["2Late Drop Begins"];
    conv.ask("The late drop period begins on " + map["2Late Drop Begins"])
})
.catch((error) => {
    console.log(error);
    conv.ask("An error occured, please try again.");
})

});

【问题讨论】:

    标签: node.js promise dialogflow-es actions-on-google cheerio


    【解决方案1】:

    问题不在于cheerio。

    您似乎正在使用request-promiserequest-promise-native 进行HTTP 调用。这将执行一个异步操作,该操作将返回一个 Promise(正如您使用 .then().catch() 所证明的那样。

    由于执行异步操作的 Intent 处理程序必须返回一个 Promise,因此您可以简单地返回由 rp/then/catch 链返回的那个。像改变这条线这样的东西应该可以工作:

    return rp(options)
    

    【讨论】:

    • 哇,原来如此,不敢相信我没想到。谢谢!
    • 很容易错过!
    【解决方案2】:

    我已修改您的代码以返回 Promise。检查这是否适合您。

    app.intent("late drop", (conv, {
      term,
      year
    }) => {
    
      return new Promise(function (resolve, reject) {
    
        var date = new Date();
        var month;
        if (term == null) {
          month = date.getMonth();
    
          if (month >= 9 && month <= 12) {
            term = "fall";
            //console.log("fall")
          } else if (month >= 1 && month <= 5) {
            term = "spring";
            //console.log("spring")
          } else {
            term = "summer";
            //console.log("summer")
          }
        }
        if (year == null) {
          yearDig = date.getFullYear();
          year = yearDig;
          //console.log(year)
        }
        var strYear = year.toString();
        var semester = term + strYear.substr(2);
        const options = {
          uri: `https://www.registrar.psu.edu/academic_calendar/${semester}.cfm`,
          transform: function (body) {
            return cheerio.load(body);
          }
        };
        rp(options)
          .then(($) => {
            let map = {};
            let columnOne = [];
            let columnThree = [];
    
            $('table').find('tr td:nth-child(1)').each(function (index, element) {
              columnOne.push($(element).text());
            });
    
            $('table').find('tr td:nth-child(3)').each(function (index, element) {
              columnThree.push($(element).text());
            });
    
            columnOne.forEach((item, i) => {
              map[item] = columnThree[i];
            });
    
            console.log(map);
            date = map["2Late Drop Begins"];
            conv.ask("The late drop period begins on " + map["2Late Drop Begins"])
            resolve()
          })
          .catch((error) => {
            console.log(error);
            conv.ask("An error occured, please try again.");
            reject()
          })
      });
    
    });
    

    希望有帮助!

    【讨论】:

      猜你喜欢
      • 2018-02-04
      • 2017-10-29
      • 1970-01-01
      • 2015-05-30
      • 1970-01-01
      • 1970-01-01
      • 2016-05-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多