Saturday, May 25, 2024
 Popular · Latest · Hot · Upcoming
23
rated 0 times [  27] [ 4]  / answers: 1 / hits: 22926  / 5 Years ago, mon, december 23, 2019, 12:00:00

I am trying to load a data from my firebase backend but getting this error


Uncaught (in promise) TypeError: response.json is not a function

my code is as below :


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 ');
}
});
};

however, if I try response.data instead of response.json it works and successfully loads the data, so I am curious what the difference is and why the first one doesn't work.


More From » vue.js

 Answers
3

Because of the axios package, it has a specific response schema https://github.com/axios/axios#response-schema



The response for a request contains the following information.

{
// `data` is the response that was provided by the server
data: {},

// `status` is the HTTP status code from the server response
status: 200,

// `statusText` is the HTTP status message from the server response
statusText: 'OK',

// `headers` the headers that the server responded with
// All header names are lower cased
headers: {},

// `config` is the config that was provided to `axios` for the request
config: {},

// `request` is the request that generated this response
// It is the last ClientRequest instance in node.js (in redirects)
// and an XMLHttpRequest instance in the browser
request: {}
}

[#51365] Saturday, December 7, 2019, 5 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mitchell

Total Points: 95
Total Questions: 110
Total Answers: 87

Location: Gabon
Member since Thu, Jul 15, 2021
3 Years ago
;