Firebase无效的文档参考。文档引用必须具有偶数个段

时间:2018-06-02 04:49:34

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

此查询有什么问题?

const db = firebase.firestore()
const query = db.doc(this.props.user.uid).collection('statements').orderBy('uploadedOn', 'desc').limit(50)

我收到以下错误:

Uncaught Error: Invalid document reference. Document references must have an even number of segments, but FrMd6Wqch8XJm32HihF14tl6Wui2 has 1
    at new FirestoreError (index.cjs.js:346)
    at Function.DocumentReference.forPath (index.cjs.js:15563)
    at Firestore.doc (index.cjs.js:15368)
    at UploadStatementPresentation.componentWillMount (UploadStatementPage.jsx:61)
    at UploadStatementPresentation.componentWillMount (createPrototypeProxy.js:44)
    at callComponentWillMount (react-dom.development.js:6872)
    at mountClassInstance (react-dom.development.js:6968)
    at updateClassComponent (react-dom.development.js:8337)
    at beginWork (react-dom.development.js:8982)
    at performUnitOfWork (react-dom.development.js:11814)

3 个答案:

答案 0 :(得分:9)

由于您尚未描述您正在尝试查询的内容,因此我只是指出所有文档必须位于集合中,无一例外。所以,如果你这样说:

db.doc(this.props.user.uid)

Firestore假定您传递给doc()的字符串包含由斜杠分隔的集合和文档ID。但在你的情况下,这似乎是不太可能的。您需要确定uid所在的集合,并在构建对要查询的集合的引用时首先使用该集合。假设你在uid文档中有一个statements子集合,而其他一些集合包含uid文档,你必须指定如下的完整路径:

db.collection('that-other-collection').doc(this.props.user.uid).collection('statements')

当然,只有您知道数据的实际结构。

答案 1 :(得分:0)

如果要获取带查询的文档集合,则不必指定文档ID。下面的代码应该适用于这种情况。

const query = db.collection('statements').orderBy('uploadedOn', 'desc').limit(50)

或者,如果您想获取文档,可以将文档ID传递给 doc()方法。在这种情况下,代码应该是。

const query = db.collection('statements').doc(this.props.user.uid)

有关查询风暴数据的更多详细信息:https://firebase.google.com/docs/firestore/query-data/get-data?authuser=0

答案 2 :(得分:0)

对于其他遇到此问题的人,请确保没有任何文档引用包含空字符串。

在使用get输入下面的uid方法时出现了这个问题,却忘记了检查uid是否为空

private fun getFullRef(uid: String): CollectionReference {
        return ref.document(uid).collection(FireContact.SUB_PATH)
    }
相关问题