【发布时间】:2021-09-27 17:42:48
【问题描述】:
我正在尝试从给定的 URL 获取一些信息。我将 URL 分配给了一个名为 URL 的 const。我使用 fetch api 从源获取信息为 JSON 格式。我无法控制即将到来的信息。这是我的代码;
const fetch = require("cross-fetch");
const URL = "https://anapioficeandfire.com/api/books"
// Important: Don't change the function name
const getBooks = async () => {
// Your code goes here
const response = await fetch(`${URL}`)
.then(res => res.json())
.then(data => console.log(data));
const books = await response.json();
return books;
}
getBooks().then(books => console.log(books))
This is the response from the code that I wrote
我只需要
{
名称:“...”,
numberOfPages: "....",
发布:“......”,
},
{
名称:“...”,
numberOfPages: "....",
发布:“......”,
},
....
【问题讨论】:
-
接收到的数据中的其他属性有什么问题?您确实还没有明确而具体地确定您的实际问题是什么
-
您正在混合使用
await和.then()语法。这导致您两次调用.json()和console.log。您的代码中应该只有一个。
标签: javascript async-await fetch