【问题标题】:Firestore then() function not executing after set()Firestore then() 函数在 set() 之后未执行
【发布时间】:2018-04-25 03:17:59
【问题描述】:

我正在使用 set 函数在 Firestore 中创建一个新文档。我想在set函数将数据推入数据库后使用文档数据。

            // Add each record to their respective heads
        let docRef = headCollectionRef.doc(data.headSlug).collection(recordsCollection)
            .doc(String(data.recordData.sNo))
        docRef.set(data).then(docSnap => {
            agreementData.push(
                docSnap.get().then(data => {
                    return data
                })
            )
        })

then() 中的任何内容都没有被执行。

非常感谢任何帮助

【问题讨论】:

    标签: node.js firebase google-cloud-firestore


    【解决方案1】:

    set 返回Promise<void>,因此您无权访问then 中的文档数据。

    在这种情况下,async/await 将使您的代码更易于阅读。

    async function cool() {
      const docRef = headCollectionRef.doc(...)
    
      await docRef.set(data)
    
      const docSnap = await docRef.get()
    
      agreementData.push( docSnap.data() )
    
    }
    

    https://firebase.google.com/docs/reference/js/firebase.firestore.DocumentReference#set

    【讨论】:

    • 这不起作用。正如问题所说,“then”不起作用,这与“then”内部的数据无关。所以问题基本上是 set 方法不会随时得到解决。因此,您在 await 之后编写的任何代码都不会被执行。至少这对我不起作用。
    猜你喜欢
    • 2019-02-22
    • 2021-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-06
    • 2018-12-31
    • 2020-11-18
    • 1970-01-01
    相关资源
    最近更新 更多