ฉันมีข้อผิดพลาดนี้เมื่อฉันรวบรวมรหัสใน node.js ฉันจะแก้ไขได้อย่างไร
RefernceError: fetch ไม่ได้ถูกกำหนดไว้
นี่คือฟังก์ชั่นที่ฉันทำมันมีหน้าที่ในการกู้คืนข้อมูลจากฐานข้อมูลภาพยนตร์เฉพาะ
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);
});
}
fetchไม่ใช่วิธี nodejs มาตรฐาน - คุณต้องการnode-fetch
fetch()ได้รับการออกแบบมาสำหรับเบราว์เซอร์และกลับไปยัง node.js ในโมดูลบุคคลที่สามซึ่งดูเหมือนว่าคุณขาดหายไป request()หรือrequest-promise()ห้องสมุดที่ถูกสร้างขึ้นโดยกำเนิดมากขึ้นสำหรับ Node.js และสนับสนุนช่วงกว้างมากของตัวเลือกสำหรับ Node.js รวมทั้งลำธารเป็น zillion วิธีการตรวจสอบ ฯลฯ ...
