最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do i check if a field-value exists across all documents in a collection in Cloud Firestore? - Stack Overflow

programmeradmin0浏览0评论

How do i check one Field in all Documents in a Collection against a Value?

I am setting up a digital tally for my coworkers to track what we take out of the fridge. we all have a 4 digit number on our Cards we would like to use to log in and then store what we took out of the pany fridge (and pay up at the end of the month)

I have one Collection that contains the Users Name and their Number. When i enter my Code i want to check the entire Collection if that entry exists.

My Firebase with Users and ID (nummer)

Following this this post Check if value exists in firebase DB i have my code set up like this:

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!");
 });  

when running the code i get an Error "ref.child is not a function". trying to figure out why i get this error i found this post: "ref.child is not a function" when adding .orderByChild.StartAt filter but was unable to find a solution to my inital question.

Kind regards.

How do i check one Field in all Documents in a Collection against a Value?

I am setting up a digital tally for my coworkers to track what we take out of the fridge. we all have a 4 digit number on our Cards we would like to use to log in and then store what we took out of the pany fridge (and pay up at the end of the month)

I have one Collection that contains the Users Name and their Number. When i enter my Code i want to check the entire Collection if that entry exists.

My Firebase with Users and ID (nummer)

Following this this post Check if value exists in firebase DB i have my code set up like this:

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!");
 });  

when running the code i get an Error "ref.child is not a function". trying to figure out why i get this error i found this post: "ref.child is not a function" when adding .orderByChild.StartAt filter but was unable to find a solution to my inital question.

Kind regards.

Share Improve this question edited Aug 23, 2019 at 14:05 Doug Stevenson 318k36 gold badges454 silver badges472 bronze badges asked Aug 23, 2019 at 12:04 SirRenderSirRender 431 silver badge3 bronze badges 3
  • 1 It looks like you are confusing firebase realtime database mands with firebaes firestore mands. There are two different types of databases hosted by firestore – chrismclarke Commented Aug 23, 2019 at 12:30
  • 2 From the screenshot I can see your database is firestore, there is documentation about querying here: firebase.google./docs/firestore/query-data/queries – chrismclarke Commented Aug 23, 2019 at 12:30
  • It seems like i got those 2 databses bixxed up. chrismclarke posted a nice piece of code that helps me out a lot. thanks for that again. – SirRender Commented Aug 26, 2019 at 13:34
Add a ment  | 

1 Answer 1

Reset to default 8

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());
    });
});

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

More information about querying in firestore can be found at: https://firebase.google./docs/firestore/query-data/queries

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论