我正在尝试建立一个语音助理新闻应用程序使用艾伦人工智能。在我的代码中,我使用了newsapi.org API。当我尝试执行代码时,它显示了2个错误。"uncaught user exception“和"Cannot read property 'length' of undefined”。如果可能的话,请大家帮助一下如何摆脱这个错误。这是我的代码
intent('What does this app do?', 'What can I do here?',
reply('This is a news project.'));
const API_KEY = 'e1f728171ee54ccd861576421b6a9fbc';
let savedArticles = [];
intent('Give me the news from $(source* (.*))', (p) => {
let NEWS_API_URL = `https://newsapi.org/v2/top-headlines?apiKey=${API_KEY}`;
if(p.source.value){
NEWS_API_URL = `${NEWS_API_URL}&sources=${p.source.value.toLowerCase().split(" ").join('-')}`
}
api.request(NEWS_API_URL, (error, response, body) => {
const {articles} = JSON.parse(body);
if(!articles.length) {
p.play('Sorry, please try searching for news from a different source');
return;
}
savedArticles = articles;
p.play({ command: 'newHeadlines', articles });
p.play(`Here are the (latest|recent) ${p.source.value}.`);
});
})发布于 2021-10-16 04:14:09
我最近才了解到这一点,问题是,当你试图从CNN或ESPN (没有"-“连字符符号的单词)中调用新闻时,它会显示未定义,
尝试先拨打BBC-news或ABC-news,然后再拨打CNN或任何其他新闻提要,这对我很管用
https://stackoverflow.com/questions/67261081
复制相似问题