【问题标题】:Pushing Response From Axios Call To Firebase - Vue.js将 Axios 调用的响应推送到 Firebase - Vue.js
【发布时间】:2018-05-21 22:22:24
【问题描述】:

我正在对 api 进行 axios 调用,并得到所需的响应。但是,我现在需要接受该响应并将其推送到我的 Firebase Firestore 中。我知道如何推送到 Firestore,但我似乎无法从 axios 调用推送中正确访问数据。这是我的代码:

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

我从 api 得到一个 json 数组,并可以在我的 vue 页面上显示它(此时它是一个对象)(所以 {{games}})。但是,我需要获取该数据,对其进行迭代,并在每次迭代中将游戏数据推送到 Firestore。你能解释一下怎么做吗?我可以编写代码推送到firestore,但我不能编写代码来迭代来自axios调用的响应以推送到firebase。我一直在尝试,但没有得到任何地方。任何提示都很棒。

谢谢!

【问题讨论】:

    标签: javascript firebase vue.js axios


    【解决方案1】:

    因为它是一个 JSON 字符串,所以你可以使用类似这样的方法对其进行迭代,以将其操作为你想要的数据结构:

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

    另外,将迭代传递给辅助函数可能是一个好主意,并带有一个以将其发送到 Firestore 的承诺结束。

    【讨论】:

    • 谢谢!经过审查,事实证明我得到了一个对象(所以数组?)。我将代码放入您拥有的代码中,并改为执行 console.log(response.data[i]) 。这在控制台中给了我这个响应: CPM0to10: Getter & Setter GPM0to10: Getter & Setter ob: Object { value: {…}, dep: {…}, vmCount: 0 } assistants: Getter & Setter Championid: Getter & Setter cs: Getter & Setter etc. 那么这是否意味着我得到了一个对象,如果是,我该怎么办?
    猜你喜欢
    • 2019-08-18
    • 2019-01-20
    • 1970-01-01
    • 1970-01-01
    • 2020-12-25
    • 2022-01-25
    • 2022-06-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多