我的意图是调用了cheerio.load()并将其与响应混淆。 Google助手不断告诉我,即使稍后在我有回复的代码中也没有设置任何回复。控制台还告诉我未将异步调用返回给处理程序,我相信它是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.");
})


});

最佳答案

问题不在于cheerio。

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

由于执行异步操作的Intent Handler必须返回Promise,因此您可以简单地返回rp / then / catch链返回的那个。如更改此行之类的东西应该起作用:

return rp(options)

关于node.js - Cheerio.load弄乱了Google Assistant的响应,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53577830/

10-11 05:07