在Firebase中添加数据列表 [英] Adding list of data in Firebase
本文介绍了在Firebase中添加数据列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下代码按预期方式工作.但是我有两个问题:
The following code works as expected. But I have 2 questions:
// Save default Account Types
var refTypes = this.housesRef.child(key + "/memberaccounttypes/");
refTypes.push({ name: 'Checking', icon: '0' });
refTypes.push({ name: 'Savings', icon: '0' });
refTypes.push({ name: 'Credit Card', icon: '0' });
refTypes.push({ name: 'Debit Card', icon: '0' });
refTypes.push({ name: 'Investment', icon: '0' });
refTypes.push({ name: 'Brokerage', icon: '0' });
- 通过这种方法,我是否要去Firebase进行几次旅行,每次推动一次?
- 是否存在一种更高效,最佳实践的方式来一次保存以下数据?
我正在使用Firebase SDK 3
I'm using Firebase SDK 3
推荐答案
以这种方式进行的每个调用确实会导致往返Firebase.您可以通过检查浏览器调试工具的网络"标签上的"Web套接字"窗格来轻松地验证这一点.
Each call to push in this way will indeed result in a roundtrip to Firebase. You can easily verify this by checking the "Web Sockets" pane on network tab of your browser debug tools.
如果您希望将其作为单个更新运行,则可以使用以下命令将它们组合成一个多位置更新:
If you'd like to run this as a single update, you can combine them into a multi-location update with:
var refTypes = this.housesRef.child(key + "/memberaccounttypes/");
var updates = {};
updates[refTypes.push().key] = { name: 'Checking', icon: '0' };
updates[refTypes.push().key] = { name: 'Savings', icon: '0' };
updates[refTypes.push().key] = { name: 'Credit Card', icon: '0' };
updates[refTypes.push().key] = { name: 'Debit Card', icon: '0' };
updates[refTypes.push().key] = { name: 'Investment', icon: '0' };
updates[refTypes.push().key] = { name: 'Brokerage', icon: '0' };
refTypes.update(updates);
请注意,由于 查看全文