reactjs - 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 实时数据库
解决方案
您是否尝试过记录来自的响应.push()
?
如果你有,你会看到密钥是返回给你的响应的一部分——你可以像普通的 JS 对象一样访问它.key
this.afd.list('listNameHere').push({
prop: value,
prop: value,
prop: value
}).then( response => {
console.log(response.key);
});
推荐阅读
- sql-server - 如何在 SQL Server 的查询中提供带有日期时间的动态表名
- flutter - Flutter:在MultiSelect中选定项目后未触发onSaved事件
- javascript - 显示单个图像和结束循环的问题
- python - 无法保持 chrome 驱动程序窗口打开
- c++ - 生成所有可能组合的列表并从列表中随机选择一个组合
- html - 导航栏的背景颜色
- python - 每次点击“开始”按钮,秒表都会变得更快
- sql - 具有集群和分区的表上的 Bigquery SQL 性能问题
- asp.net - 在 ASP.net Web 服务中设置 Content-Type
- python - 使用 seaborn 和 matplotlib 对热图进行注释的绘图