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

9
这个查询有什么问题?
const db = firebase.firestore()
const query = db.doc(this.props.user.uid).collection('statements').orderBy('uploadedOn', 'desc').limit(50)

I get the following error:

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)

请使用集合和文档描述您的数据库布局,以便访问此查询。 - Doug Stevenson
3个回答

27

由于您没有描述您正在尝试查询什么,我只想指出所有文档必须在集合中,没有例外。 因此,如果您输入以下内容:

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')

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


0

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

当使用以下带有uid输入的get方法并忘记检查uid是否为空时,我遇到了这个问题。

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

0
如果您想通过查询获取文档集合,就不必指定文档ID。在这种情况下,下面的代码应该可以工作。
const query = db.collection('statements').orderBy('uploadedOn', 'desc').limit(50)

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

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

有关查询 firestorm 数据的更多细节,请参阅:https://firebase.google.com/docs/firestore/query-data/get-data?authuser=0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接