代码之家  ›  专栏  ›  技术社区  ›  tomen

如何在FireBase中推送多个项目?

  •  0
  • tomen  · 技术社区  · 7 年前

    我有一组对象。我尝试做的是将所有项目推送到数据库中的特定位置,因为通过推送调用,每个项目都将被分配一个唯一的ID。

    我试着把FireBase Push放在一个循环中,但我认为这样做是不对的。 (这里的Web应用程序)

    const array = [{title: 'one'},{title:'two'}, {title: 'three'}]
    
    array.map(item => {
              FirebaseRef.child(`boards/${boardId}/containers`).push(item)
    })
    
    1 回复  |  直到 7 年前
        1
  •  2
  •   Renaud Tarnec    7 年前

    下面,使用 update() 方法,将起作用:

    const array = [{ title: 'one' }, { title: 'two' }, { title: 'three' }]
    
    var updates = {};
    array.map(item => {
         var newPostKey = firebase.database().ref().child(`boards/${boardId}/containers/`).push().key;
         updates[`boards/${boardId}/containers/` + newPostKey] = item;
    });
    firebase.database().ref().update(updates);