一次推送多个对象

Push multiple objects at once

如何使用 angularfire2 一次执行多个对象推送?

仅仅推送对象数组并不会为每个对象设置键。

this.af.database.list('/symbols/').push({
  typ: "symbol1",
  // ....
});
this.af.database.list('/symbols/').push({
  typ: "symbol2",
  // ....
});

使用常规 Firebase JavaScript SDK,您可以通过以下方式完成此操作:

var updates = {};
updates['/symbols/'+ref.push().key] = {
  typ :"symbol1", ....
};
updates['/symbols/'+ref.push().key] = {
  typ :"symbol2", ....
};
ref.update(updates);

由于 AngularFire2 构建在普通 Firebase JavaScript SDK 之上,因此它们可以完美互操作。因此,您可以只使用 Firebase JavaScript SDK 进行此操作。

根据@Frank 的回答,即兴创作了一个 reducer 和一个 promise resolver> 这是最终版本:

const admin = require('firebase-admin');
const types = [
  {
    type: "symbol1"
  },
  {
    type: "symbol2"
  }
];
const rootRef = admin.database().ref();
const ref = rootRef.child(`/symbols`);

// prepare updates object based on types array.
let updates = types.reduce((update, type) => {
  let id = ref.push().key;
  update[`/symbols/${id}`] = type;
  return update;
}, {});

await rootRef.update(updates)
.then((response) => {
  return res.status(201).json({message: "Records added successfully"});
})
.catch((error)=>{
  res.sendStatus(500);
})