目前我正在学习 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;
}
}
}
我可以查询您的 Firestore 数据库(因为我们可以看到您的项目 ID),您的文档的确切 ID 似乎是“giMXcFmLUxfCaCmyYo0TJFeEhBL2”,而不是“giMXcFmLUxfCaCmyYo0TJFeEhBL2”:第一个字符是空格。
奇怪(且具有误导性)的是,Firestore 控制台在显示集合中的文档 ID 时不显示空格(请参见下图中的红色矩形,这是我的一个项目中的一个示例),但是如果您单击在显示文档路径的区域,您可以看到空格(见下图中的蓝色矩形)。
我可以通过使用以下命令查询集合来发现这一点:
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);
});
(我建议您保护 Firestore DB,因为 Firebase 项目 ID 已发布)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)