4

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 company 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.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
SirRender
  • 43
  • 1
  • 3
  • 1
    It looks like you are confusing firebase realtime database commands with firebaes firestore commands. There are two different types of databases hosted by firestore – chrismclarke Aug 23 '19 at 12:30
  • 2
    From the screenshot I can see your database is firestore, there is documentation about querying here: https://firebase.google.com/docs/firestore/query-data/queries – chrismclarke Aug 23 '19 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 Aug 26 '19 at 13:34

1 Answers1

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.com/docs/firestore/query-data/queries

chrismclarke
  • 1,995
  • 10
  • 16
  • 1
    Thanks you a lot. your code works and does what i need it to do. – SirRender Aug 26 '19 at 13:33
  • Are you using firebase auth? If so you can subscribe to login change events to know if successful instead of waiting an arbitrary amount of time. What list are you talking about exactly? The list of login ids? – chrismclarke Sep 02 '19 at 09:12