如何在Nuxt中向Firebase实时数据库写入指定结构的数据?
解决Firebase Realtime Database写入错误的正确方法
你出错的核心原因是用错了API:put()是Firebase Storage用于上传文件的方法,不是Realtime Database的写入接口,所以调用后会触发错误。
要实现你想要的数据库结构,可根据需求选择以下两种正确写法:
1. 覆盖整个hero_page节点
如果需要完全替换hero_page下的所有数据(不管节点原有内容),使用set()方法:
const data = { title: this.title, subtitle: this.subtitle, image: imageUrl } this.$fire.database.ref('hero_page').set(data)
2. 更新指定字段
如果只想更新hero_page下的目标字段(保留节点内原有其他数据),使用update()方法:
const data = { title: this.title, subtitle: this.subtitle, image: imageUrl } this.$fire.database.ref('hero_page').update(data)
执行上述代码后,你的Realtime Database结构会变成预期的样子:
hero_page: title: test_title, subtitle: test_subtitle, image: test_url_of_image
内容的提问来源于stack exchange,提问作者Rustam Ravshanovich
相关产品推荐
相关产品推荐

