Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/444.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript fetch API如何通过在函数中传递JSON对象将书籍标题呈现到DOM中_Javascript_Api_Fetch - Fatal编程技术网

Javascript fetch API如何通过在函数中传递JSON对象将书籍标题呈现到DOM中

Javascript fetch API如何通过在函数中传递JSON对象将书籍标题呈现到DOM中,javascript,api,fetch,Javascript,Api,Fetch,我有一个实验室,需要使用fetch()获取数据 // To pass the tests, don't forget to return your fetch! fetch('https://anapioficeandfire.com/api/books') .then(function(response) { return response.json(); }) .then(function(titles) { return titles }); renderBooks(titles)

我有一个实验室,需要使用fetch()获取数据

// To pass the tests, don't forget to return your fetch!
fetch('https://anapioficeandfire.com/api/books')
.then(function(response) {
  return response.json();
})
.then(function(titles) {
  return titles
});
renderBooks(titles)
}

function renderBooks() {
const main = document.querySelector('main');
books.forEach(book => {
  const h2 = document.createElement('h2');
  h2.innerHTML = book.name;
  main.appendChild(h2);
});
}

document.addEventListener('DOMContentLoaded', function() {
fetchBooks();
});

我收到的错误是没有定义标题。目标是通过将JSON对象传递给renderBooks(),将书籍标题呈现到DOM中。

这是否回答了您的问题?这个例子有一些地方不对劲
renderBooks
不接受参数<代码>渲染书(标题)在
然后
之外<代码>获取书籍
未定义。