0

I have been trying to solve this problem for several days… Any help is greatly appreciated.

I am trying to build a Firebase Cloud Function that is triggered from a Pub/sub topic (‘ETACheck’). The triggering part is working fine. When the cloud function is invoked, it scans the Firebase database looking for all records where ’ECT’ key equals to ‘’. Here is how the data is organized:

enter image description here

Here is the code snippet:

exports.CheckETAResponse = functions.pubsub.topic('ETACheck').onPublish((message, context) => {
   console.log('The function was triggered at ', context.timestamp);
   console.log('The unique ID for the event is', context.eventId);
   var db = admin.database();
    var ref = db.ref('{company}/Requisitions/{store}');
    return ref.orderByChild('ECT').equalTo('').on('value')
        .then((snapshot) => {
        if (snapshot !== null) {
            const data = snapshot.val();
            console.log('Data',data);
        }
        else {
            console.log('Snapshot is null');
        }
        return null;
    })
    .catch((error) => console.log('Error',error))

});

And the error from Firebase (data = null):

enter image description here

I changed the query to “.on(‘child_added’), and I got the following error:

enter image description here

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
S.Hua
  • 69
  • 4
  • 1
    Please don't link to images of text. Copy the text directly into the question. – Doug Stevenson Jun 17 '19 at 17:30
  • You've included a picture of the JSON tree in your question. Please replace that with the actual JSON as text, which you can easily get by clicking the Export JSON link in the overflow menu (⠇) of [your Firebase Database console](https://console.firebase.google.com/project/_/database/data/). Having the JSON as text makes it searchable, allows us to easily use it to test with your actual data and use it in our answer and in general is just a Good Thing to do. – Frank van Puffelen Jun 17 '19 at 18:42
  • In this code `db.ref('{company}/Requisitions/{store}')`, what do you think `{company}` and `{store}` do? Firebase does not automatically expand these into anything, so will just treat then as literal strings. – Frank van Puffelen Jun 17 '19 at 18:44
  • @Frank van Puffelen, I thought '{xxx}' represents a wildcard. I was trying to find all records for any company or store that have ECT = ''. – S.Hua Jun 17 '19 at 21:26
  • Those types of wildcards are only possible in Cloud Functions declarations, not in other paths to the database. I'll write an answer. – Frank van Puffelen Jun 17 '19 at 21:36

1 Answers1

0

Those types of wildcards in db.ref('{company}/Requisitions/{store}') are only possible in Cloud Functions declarations, not in other paths to the database. Firebase queries work on a model of a flat list of child nodes. There is no way to find all nested child nodes under Requisitions that have ECT == "". You can search under Requisitions/001, but not under all Requisitions/*.

Consider adding an inverted index that allows this type of lookup:

EmptyECTs: {
  "Requestion/001/-Lhv...": true
}

That allows you to easily read the nodes.

For more on this, also see:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • I am not following you about your suggestion to add an inverted index. Companies and stores are dynamically created, so I won't know what they will be ahead of time. I do have access to the list of all companies and their corresponding stores that I can replace the db.ref() wildcard argument. – S.Hua Jun 17 '19 at 22:28
  • I just replaced the wild cards with static strings and I am getting data when I use: .on('child_added') or .once('child_added'). Thx! – S.Hua Jun 17 '19 at 22:52