Sir*_*der 2 javascript firebase google-cloud-firestore
如何根据值检查集合中所有文档中的一个字段?
我正在为我的同事设置一个数字计数,以跟踪我们从冰箱中取出的物品。我们的卡上都有一个 4 位数字,我们想用它来登录,然后存储我们从公司冰箱中取出的东西(并在月底付款)
我有一个包含用户名及其编号的集合。当我输入我的代码时,我想检查整个集合是否存在该条目。
在这篇文章之后检查firebase DB中是否存在值, 我的代码设置如下:
ref.child("nutzer").orderByChild("nummer").equalTo("1337").once("value",snapshot => {
if (snapshot.exists()){
const userData = snapshot.val();
console.log("exists!", userData);
}
else{
console.log("does not exists!");
});
Run Code Online (Sandbox Code Playgroud)
运行代码时,我收到错误“ref.child 不是函数”。试图弄清楚为什么我会收到这个错误,我发现了这个帖子:“ref.child is not a function” when 添加 .orderByChild.StartAt 过滤器 但无法找到我最初问题的解决方案。
亲切的问候。
The code you have used is from firebase realtime database, but your data is stored in firebase firestore (firebase provides two different types of databases)
Firestore has recently added collection group queries which can search across multiple collection documents, e.g.
var nummers = db.collectionGroup('nutzer').where('nummer', '==', '1337');
nummers.get().then(function (querySnapshot) {
querySnapshot.forEach(function (doc) {
console.log(doc.id, ' => ', doc.data());
});
});
Run Code Online (Sandbox Code Playgroud)
To test whether the data exists, you could simply date the initial query snapshot before processing and check the .empty property. E.g.
nummers.get().then(function (querySnapshot) {
return !querySnapshot.empty
})
Run Code Online (Sandbox Code Playgroud)
More information about querying in firestore can be found at: https://firebase.google.com/docs/firestore/query-data/queries
| 归档时间: |
|
| 查看次数: |
3789 次 |
| 最近记录: |