简体   繁体   中英

Pushing Response From Axios Call To Firebase - Vue.js

I am doing an axios call to an api and am getting the desired response. However, I now need to take that response and push it up into my firebase firestore. I know how to push into the firestore, but I can't seem to access the data correctly from the axios call to push. Here is my code:

axios.get('https://somewhereontheweb.com/api/getgames', {
          headers: {
            'Content-Type': 'application/json',
            'Accept': 'application/json',
            'Authorization': 'Bearer ' + token,
          }
        })
          .then(response => {
            this.games = response.data;

            // push data into firestore?
          })
          .catch(function (error) {
            console.log(error);
          });

I get a json array back from the api and can display it on my vue page (at this point it is an object) (so {{games}}). However, I am needing to take that data, iterate over it and in each iterations push the game data into firestore. Can you explain how to do that? I can write the code to push into firestore, but I can't write the code to iterate over the response from the axios call to push into firebase. I have been trying but am not getting anywhere. Any hint is great.

Thanks!

As it is a JSON string you are a getting then you can use something like this to iterate over it to manipulate it into the data structure you want:

for(var i in response.data){
  response.data[i].dosomething()
}

Also, might be a good idea to pass the iteration to a helper function with a promise that ends in sending it to Firestore.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM