0

I am using javascript to write a Google Cloud function. In the onWrite function I am trying to retrieve a list of subscriptions from using a where clause on the 'subscription' collection. Each subscription doc looks like this:

Subscription Collection Structure

I need to use the Ref field under 'ToUser' in the where clause so I can match it with the userRef I obtained from the onWrite cloud function trigger -> 'userRef' which looks like this:

let userRef = "/users/0e2qgHKZokSb9l02EywrEmJNhVd2"

Here is the js code I have to try and query the subscription collection:

// 2. Query subscription collection using above id to 'ToUser'
// for list of subscribers

let subscriptions = db.collection("subscriptions")

let subscribers = subscriptions.where('ToUser.Ref', '==', userRef).get()
.then(snapshot => {
    if (snapshot.empty) {
        console.log('No matching documents.');
        return;
    }  
    var subs = []

    snapshot.forEach( sub => {
        console.log("Sub : " + JSON.stringify(sub.FromUser.Ref) )
        //subscribers.append(sub.val().FromUser)
    })
    return subs
}).catch(err => {
    console.log('Error getting documents', err);
});

The result of the call to .get().then() is snapshot is empty and the console.log('No matching documents.') is called.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Weston Mitchell
  • 192
  • 1
  • 12

1 Answers1

1

When comparing to a reference, you should pass in an actual DocumentReference object.

So:

let userRef = db.doc("/users/0e2qgHKZokSb9l02EywrEmJNhVd2")
let subscriptions = db.collection("subscriptions")

let subscribers = subscriptions.where('ToUser.Ref', '==', userRef).get()
  ...
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807