Cheerio.load弄乱了Google Assistant的响应 [英] Cheerio.load messes with responses for Google Assistant

查看:62
本文介绍了Cheerio.load弄乱了Google Assistant的响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的意图是调用了cheerio.load(),并且使响应混乱。 Google助手不断告诉我,即使稍后在我有回复的代码中也没有设置任何回复。控制台还告诉我,异步调用没有返回到处理程序,我相信它是cheerio.load()。无论如何,我可以解决此问题,以便它继续在代码底部查找正确的conv.ask吗?它仍然继续向下运行,直到显示console.log(map)。谢谢您的帮助!

I have this intent that has a cheerio.load() being called and it messes with with the responses. The Google Assistant keeps telling me that no response has been set even though later down in the code I have responses. The console is also telling me that an async call wasn't being returned to the handler, which I believe is the cheerio.load(). Is there anyway I can fix this so that it continues looking for the correct conv.ask at the bottom of the code? It still continues to run down to there too, for the console.log(map) shows up. Thanks for any help!

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.");
})

});

推荐答案

问题不在于cheerio。

The issue is not with cheerio.

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

It looks like you are using request-promise or request-promise-native to make your HTTP call. This does an asynchronous operation that will return a Promise (as is evidenced by your use of .then() and .catch().

由于执行异步操作的Intent处理程序必须返回Promise,因此您只需返回rp返回的那个/ then / catch链。更改此行之类的方法应该起作用:

Since Intent Handlers that do asynchronous operations must return a Promise, you can simply return the one that is returned by the rp/then/catch chain. Something like changing this line should work:

return rp(options)

这篇关于Cheerio.load弄乱了Google Assistant的响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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