【问题标题】:Getting a Cloud Firestore document reference from a documentSnapshot从 documentSnapshot 获取 Cloud Firestore 文档参考
【发布时间】:2018-08-02 17:18:52
【问题描述】:

问题

我正在尝试从查询中检索文档引用。我的代码返回undefined。我可以通过提取documentSnapshot.ref的各个部分来获得路径,但这并不简单。

我想要返回的是一个引用,以后我可以将其用于.update 文档,而无需指定集合并使用documentSnapshot.id

path 属性的文档是 here

我的代码

const db = admin.firestore();

return db.collection('myCollection').get().then(querySnapshot => {
  querySnapshot.forEach(documentSnapshot => {
    console.log(`documentReference.id   = ${documentSnapshot.id}`);
    console.log(`documentReference.path = ${documentSnapshot.path}`);
    // console.log(`documentReference.ref = ${JSON.stringify(documentSnapshot.ref)}`);
  });
});

输出

documentReference.id   = Jez7R1GAHiR9nbjS3CQ6
documentReference.path = undefined
documentReference.id   = skMmxxUIFXPyVa7Ic7Yp
documentReference.path = undefined

【问题讨论】:

    标签: javascript firebase google-cloud-firestore firebase-admin


    【解决方案1】:

    在您的代码中,documentSnapshotDocumentSnapshot 类型的对象。看起来您假设它是DocumentReference 类型的对象。 A 引用的目的是定位文档。快照的目的是在查询文档后接收文档的内容——它们绝对不是一回事。 DocumentSnapshot 没有 path 属性。

    如果您想要在 DocumentSnapshot 中获取的文档的 DocumentReference,您可以在快照中使用 ref。然后你可以得到 ref 的 path 属性:

    documentSnapshot.ref.path
    

    【讨论】:

    • 嘿道格。感谢那。我查看了 DocumentSnapshot 和 DocumentReference 文档,显然有片刻的无知。现在一切正常。我以前用过 documentSnapshot.ref.path,但找不到我用过的地方。
    • 在python中,它是reference而不是ref
    猜你喜欢
    • 2021-03-25
    • 1970-01-01
    • 1970-01-01
    • 2018-06-16
    • 2018-09-26
    • 2018-03-17
    • 1970-01-01
    • 2018-03-19
    相关资源
    最近更新 更多