【问题标题】:How to push multiple data in same object?如何在同一个对象中推送多个数据?
【发布时间】:2020-04-17 10:44:12
【问题描述】:

我需要将 id 从 firebase 推送到同一个对象中。问题是我需要保存在同一个对象中而不是新对象中。

db.collection("form").where("posted_at", ">=", 1)
.get()
.then(querySnapshot => {
querySnapshot.forEach(doc=> {
    console.log(doc.id, " => ", doc.data());
    this.array.push(doc.data()); //if i do something like this: this.array.push(doc.data(),{id:doc.id}); it saves but in new object
});
})

【问题讨论】:

    标签: javascript node.js firebase vue.js firebase-realtime-database


    【解决方案1】:
    db.collection("form").where("posted_at", ">=", 1)
    .get()
    .then(querySnapshot => {
      querySnapshot.forEach(doc=> {
        console.log(doc.id, " => ", doc.data());
        this.array.push({...doc.data(), id: doc.id});
      });
    })
    

    或者,如果您的平台不支持spread syntax,您可以像这样使用Object.assign()

    db.collection("form").where("posted_at", ">=", 1)
    .get()
    .then(querySnapshot => {
      querySnapshot.forEach(doc=> {
        console.log(doc.id, " => ", doc.data());
        this.array.push(Object.assign({}, doc.data(), {id: doc.id}));
      });
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-24
      • 1970-01-01
      • 2020-02-05
      • 1970-01-01
      • 2017-08-09
      • 2018-02-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多