我在使用 Firebase 的新 Firestore 时遇到问题。
情况:我有一个collection('room')
我创建房间collection('room').add(room)
我正在尝试做的事情:我需要更新一个房间。
为此,我使用:collection('room').doc(ROOM_ID).update(update)
所以我需要在我的集合中的文档中添加 ROOM_ID :
|room
ROOM_ID
id:ROOM_ID,
someContent: ForTheQuery
有没有可能的方法来实现这一目标?
另一种方法是为自己创建一个生成的 ID:
collection('room')
.doc(someId)
.set({
id: someId,
someContent: ForTheQuery
});
但我想避免它。
您可以使用doc() https://firebase.google.com/docs/reference/js/firebase.firestore.CollectionReference#doc创建对具有唯一 ID 的文档的引用,但该文档尚未创建。然后,您可以使用文档参考中提供的唯一 ID 设置该文档的内容:
const ref = store.collection('users').doc()
console.log(ref.id) // prints the unique id
ref.set({id: ref.id}) // sets the contents of the doc using the id
.then(() => { // fetch the doc again and show its data
ref.get().then(doc => {
console.log(doc.data()) // prints {id: "the unique id"}
})
})
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)