Skip to content
Advertisement

How can I read data from firestore database in a schedule function? [closed]

I’m new to Firebase and I’m blocked on something. Actually, I’ve some difficulties reading data from a Firestore Database. My code:

const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp();
const db = admin.firestore();

exports.scheduledFunction = functions.pubsub.schedule("* * * * *").onRun(async () => {
  console.log("start");
  const querySnapshot = await db.collection("Next_callenges").get();
  console.log("Let's see :", querySnapshot);
  return null;
});

There is no output except this : “let’s see : QuerySnapshot{“.

To add some context, the objective behind this code is to get the first data inserted in the first database, add it to a second database and delete it from the first one.

Advertisement

Answer

As others have commented, the output you get is exactly what is expected from your code: since you log querySnapshot, you get whatever debug output the QuerySnapshot class defines.

If you want to see the data of each document in that query snapshot, you can do:

querySnapshot.forEach((doc) => {
  console.log("Document "+doc.id+":", doc.data());
})

Note that this is just using the Firestore API and has nothing to do with the fact that you use Firestore in Cloud Functions. Since Cloud Functions adds quite some complexity to the case, I’d recommend first learning more about the Firestore API in JavaScript by reading its documentation and following its codelab.

Advertisement