javascript - JavaScript在变量中获取API JSON数据?
问题描述
JS新手在这里。我想将来自 API 调用的 JSON 数据存储在我的 .fetch 方法中的变量中。
这是我当前的代码:
// Store database to array
let database = []
fetch('./database.json')
.then(response => response.json())
.then(data => console.log(data))
数据已成功记录在控制台中,但我希望将它们保存在数据库数组中。
知道我会怎么做吗?非常感谢大家!
解决方案
当然,将您的代码更改为以下内容:
// Store database to array
let database = []
fetch('./database.json')
.then(response => response.json())
.then(data => {
database.push(data)
console.log(database)
})
我希望这能解决你的问题。
推荐阅读
- database - 使用 url 在 airtable 数据库中查询数据
- c - 为什么变量在块结束后会失去其价值?
- javascript - 将代理应用于对象
- javascript - 启动 WebGL 统一游戏时出错
- android - 无法集成将数据在一个片段之间传递到另一个片段的视图模型,最终将其发送到recycleviewadapter
- typescript - TSLint:表达式的类型为“void”。将其作为声明放在自己的行中
- excel - 使用 Excel 用户表单搜索 Access 数据库并使用 VBA 在列表框中显示结果
- python - 如何为自制模块设置 python 路径?
- r - 有没有办法 dplyr (tidyverse) 映射我的数据集,找到以相同后缀结尾的列然后只保留一个?
- c# - C# Windows.Forms.Timer 以错误的顺序工作