首页 > 解决方案 > firebase中的多条记录,在回调函数中重用之前生成的id

问题描述

我想做这样的事情

//doing some stuffs
const Adress = {
street: 'strong',
town: 'kinshasa'
}

const Student = {
  name:'ghost',
  phone_number:'081xxxxxxx'
}

MyFirebaseAdressRef.push(Adress, () => 
  {
    //get the genererated key by firebase
    Adress.Id = //the generated key
    Student.adress = Adress
    MyFirebaseStudentRef.push(Student)    
  })
 //doing some stuffs

如何在回调函数中获取生成的密钥。我正在使用 react 和 firebase 实时数据库

标签: reactjsfirebasefirebase-realtime-database

解决方案


您是否尝试过记录来自的响应.push()

如果你有,你会看到密钥是返回给你的响应的一部分——你可以像普通的 JS 对象一样访问它.key

this.afd.list('listNameHere').push({
  prop: value,
  prop: value,
  prop: value
}).then( response => {
   console.log(response.key);
});

推荐阅读