【问题标题】:doc.data() is not a function error after creating doc and then setting it in firestoredoc.data() 在创建 doc 然后在 firestore 中设置后不是函数错误
【发布时间】:2021-01-30 14:38:13
【问题描述】:

我有一个查询文档并返回该文档的函数。如果我对此使用 doc.data() ,它会按预期工作。

如果查询为空,因此文档不存在,它会创建文档(存储在变量中),设置它,然后返回文档。


const snapshot = await db.collection('...').where('name', '==', name)

if (snapshot.empty){
   const doc = await db.collection('...').doc()
   await doc.set({...})
   console.log(doc.data()) /// this crashes
   return doc 
} 
console.log(snapshot.docs[0].data()) // this works fine!!! 
return snaphot.docs[0]

我已经在代码旁边的 cmets 中放入了哪些崩溃和哪些不崩溃。我想知道这是怎么回事!!!

【问题讨论】:

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


    【解决方案1】:

    docDocumentReference,但没有方法data()。只能从DocumentSnapshot 检索文档数据,要从文档引用中获取此信息,您必须调用get() 方法。

    • get() - 读取此 DocumentReference 引用的文档。

    所以试试下面的sn-p,

    var doc_snap = await doc.get()
    console.log(doc_snap.data())
    

    【讨论】:

    • 非常感谢 Muthu!欣赏它:)
    猜你喜欢
    • 1970-01-01
    • 2017-12-03
    • 1970-01-01
    • 2017-12-09
    • 2020-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多