在 node.js 中编译代码时出现此错误,我该如何解决?
ReferenceError:未定义提取
这是我正在做的功能,它负责从特定的电影数据库中恢复信息。
function getMovieTitles(substr){
pageNumber=1;
let url = 'https://jsonmock.hackerrank.com/api/movies/search/?Title=' + substr + "&page=" + pageNumber;
fetch(url).then((resp) => resp.json()).then(function(data) {
let movies = data.data;
let totPages = data.total_pages;
let sortArray = [];
for(let i=0; i<movies.length;i++){
sortArray.push(data.data[i].Title);
}
for(let i=2; i<=totPages; i++){
let newPage = i;
let url1 = 'https://jsonmock.hackerrank.com/api/movies/search/?Title=' + substr + "&page=" + newPage;
fetch(url1).then(function(response) {
var contentType = response.headers.get("content-type");
if(contentType && contentType.indexOf("application/json") !== -1) {
return response.json().then(function(json) {
//console.log(json); //uncomment this console.log to see the JSON data.
for(let i=0; i<json.data.length;i++){
sortArray.push(json.data[i].Title);
}
if(i==totPages)console.log(sortArray.sort());
});
} else {
console.log("Oops, we haven't got JSON!");
}
});
}
})
.catch(function(error) {
console.log(error);
});
}
原文由 jasa1704 发布,翻译遵循 CC BY-SA 4.0 许可协议
如果您使用的是 18 之前的 Node 版本,则 获取 API 不是开箱即用的,您需要为此使用外部模块,例如 node-fetch 。
像这样将它安装在你的 Node 应用程序中
然后将下面的行放在您使用获取 API 的文件的顶部: