'Getting a specific document from query Using Firebase Cloud functions

Im trying to get a user from a users collection with the most votes on a timed function. Ive been following the examples they have on the documentation, but still having trouble. Any thoughts?

import * as functions from "firebase-functions";
import * as admin from "firebase-admin";
admin.initializeApp();

exports.testData = functions.pubsub.schedule("30 14 * * *")
    .timeZone("America/New_York")
    .onRun((context) => {
      const test = admin.firestore().collection("users").
          orderBy("votes").limit(1);

      try {
        console.log("document data:", test.data());
      } catch (e) {
        console.log("Error getting document:", e);
      }
      return null;
    });


Solution 1:[1]

Your code hasn't executed the query at all. It is just building a query object, then calling a method data() which doesn't exist on that object (likely causing your code to crash). I suggest reviewing the documentation to learn how to use get() to execute the query. It returns a promise that you must handle correctly.

return test.get()
    .then(snapshot => ...)
    .catch(error => ...)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Doug Stevenson