Tôi gặp lỗi này khi biên dịch mã của mình trong tệp node.js, làm cách nào để khắc phục?
RefernceError: tìm nạp không được xác định
Đây là chức năng tôi đang làm, nó chịu trách nhiệm khôi phục thông tin từ cơ sở dữ liệu phim cụ thể.
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);
});
}
fetchkhông phải là một phương thức nodejs tiêu chuẩn - bạn cầnnode-fetch
fetch()được thiết kế cho trình duyệt và sau đó được chuyển ngược lại sang node.js trong mô-đun của bên thứ ba mà bạn dường như đang thiếu. Các request()hoặc request-promise()thư viện được natively hơn xây dựng cho Node.js và hỗ trợ một phạm vi rộng hơn các tùy chọn cho Node.js bao gồm suối, một zillion phương pháp xác thực, vv ...
