我需要使用一个具有Firebase唯一键的请求在Firebase中存储一组对象(不使用数组索引作为键)
let object_list = {
'0': {
'title':'title 1',
'time':'12.30pm',
},
'1': {
'title':'title 2',
'time':'12.30pm',
},
'2': {
'title':'title 3',
'time':'12.30pm',
},
}
this.agendaRef = firebase.database().ref('objs/');
let data = this.agendaRef.push();
data.set(object_list);
我希望输出
-LioB1b06T7tfVhbJmss6o
time: "12.30pm"
title:"title 1"
-LioB1b06T7tfVhbsdfsdf
time: "12.30pm"
title:"title 2"
-LioB1b06Tsdsdsd7tfVh2
time: "12.30pm"
title: "title 3"
但实际输出是
-LioB1b06T7tfVhbJm6o
0
time: "12.30pm"
title:"title 1"
1
time: "12.30pm"
title:"title 2"
2
time: "12.30pm"
title: "title 3"
答案 0 :(得分:0)
每次调用push
都会生成一个ID。因此,如果您需要多个生成的ID,则需要多次调用push
。
要认识的关键是,不带任何参数的调用push()
是仅客户端操作。因此,您可以调用push()
三次以生成ID,并且仍然只执行一次写操作,如下所示:
let object_list = [
{
'title':'title 1',
'time':'12.30pm',
},
{
'title':'title 2',
'time':'12.30pm',
},
{
'title':'title 3',
'time':'12.30pm',
}
}]
this.agendaRef = firebase.database().ref('objs/');
let object = {};
object_list.forEach((value) => {
object[this.agendaRef.push().key] = value;
})
data.set(object);