Firebase - Firestore - get key with collection.add()

后端 未结 4 744
渐次进展
渐次进展 2020-12-01 16:31

I am facing a problem with the new Firestore from Firebase.

Situation: I have a collection(\'room\')

I create room with c

4条回答
  •  渐次进展
    2020-12-01 17:02

    You can use doc() to create a reference to a document with a unique id, but the document will not be created yet. You can then set the contents of that doc by using the unique id that was provided in the document reference:

    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"}
        })
    })
    

提交回复
热议问题