目前,我正在学习Firestore,并且很难编写一个查询。我希望针对giMXcFmLUxfCaCmyYo0TJFeEHBL2文档中的所有字段。我的查询如下:
if (userId) {
firebase
.firestore()
.collection('userLists')
.doc(userId)
.get()
.then(docRef => {
console.log('DATA', docRef.data());
})
.catch(error => {
console.log('Nothing here');
});
}
docRef.data()一直说它是未定义的。我做错了什么(为可能愚蠢的问题道歉..)。我的目标是得到所有的对象。
2gzrNnVjpKOGLz2lxT6n
id: "2gzrNnVjpKOGLz2lxT6n"
time: 31 January 2021 at 00:00:00 UTC+1
安全规则:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if false;
}
match /lists/{document=**} {
allow read, write: if true;
}
match /users/{document=**} {
allow read, write: if true;
}
match /userLists/{document=**} {
allow read, write: if true;
}
}
}
发布于 2021-01-31 06:45:54
我可以查询您的Firestore数据库(因为我们可以看到您的项目ID),文档的确切ID似乎是“giMXcFmLUxfCaCmyYo0TJFeEhBL2”,而不是"giMXcFmLUxfCaCmyYo0TJFeEhBL2":第一个字符是空格。
奇怪(且具有误导性)的是,Firestore在集合中显示doc时没有显示空格(请参阅下面图像中的红色矩形,这是我的一个项目中的一个示例),但是如果单击显示文档路径的区域,您可以看到空格(见下面的图像中的蓝色矩形)。
通过使用以下方法查询集合,我可以发现:
firebase
.firestore()
.collection('userLists')
.get()
.then(function (querySnapshot) {
querySnapshot.forEach(function (doc) {
// doc.data() is never undefined for query doc snapshots
console.log(doc.id, ' => ', doc.data());
});
})
.catch(function (error) {
console.log('Error getting documents: ', error);
});
(由于Firebase项目ID已经发布,我建议您确保Firestore的安全)
https://stackoverflow.com/questions/65979118
复制相似问题