我正在使用 react 和 firebase 实时数据库。我想为每个用户创建一个列表。稍后在存储中查找文件的 id 列表。在这种情况下,生成和添加 id 以在数据库中列出的最佳方法是什么?(不是数据列表,数据库中存储的数据就是列表)
所以我想到了两种方法。我会写伪代码
//Method 1
//get id list
receivedList = getKeyListFromDB()
//generate ID
newID = generateID()
// append new id
receivedList.append(newID)
//set value with updated list
updateDB(receivedList)
//Method 2
// Using the list provided by the firebase
newRef = firebase.database().ref().child('pathToList').push()
newRef.set({
key: newRef.key
})
我打算使用方法 1,但找不到生成 id 的方法。检索列表以更新它似乎效率低下。所以我试图找到另一种方法。但我找不到一个好的方法来做到这一点。
将值添加到数据库中的列表的解决方案
我已经使用事务方法解决了。但如果有办法通过使用解决,push请回答。
不负相思意
开满天机
白衣非少年
相关分类