[英]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 解析器>這是最終版本:
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);
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.