我正在尝试从我的 firebase 后端加载数据但收到此错误
Uncaught (in promise) TypeError: response.json is not a function
我的代码如下:
import axios from 'axios';
export const loadData = ({ commit }) => {
console.log('getting data from server...');
axios
.get('data.json')
.then(response => response.json())
.then(data => {
if (data) {
const stocks = data.stocks;
const stockPortfolio = data.stockPortfolio;
const funds = data.funds;
const portfolio = {
stockPortfolio,
funds
};
commit('SET_STOCKS', stocks);
commit('SET_PORTFOLIO', portfolio);
console.log('Commit done ');
}
});
};
但是,如果我尝试 response.data
而不是 response.json
它可以工作并成功加载数据,所以我很好奇有什么区别以及为什么第一个不起作用。
原文由 skr 发布,翻译遵循 CC BY-SA 4.0 许可协议
由于 axios 包,它有一个特定的响应模式 https://github.com/axios/axios#response-schema