Ad

How To Get Data From Firestore To Google Cloud Functions?

My index.js file:

const functions = require('firebase-functions');
const admin = require('firebase-admin');

const Firestore = require('@google-cloud/firestore');
const firestore = new Firestore();

admin.initializeApp();

const db = admin.firestore();

 exports.getName = functions.https.onCall((data, context) => {
    var docRef = db.collection("dogs").doc("{data.id}");
    var getDoc = docRef.get().then(doc => {
        return doc.get("name");
    })
 });

Code in the flutter project:

HttpsCallable callable = FirebaseFunctions.instance.httpsCallable("getName");
var temp = await callable({"id": "11"});
print(temp.data);

The program prints out null, even though document in collection "dogs" with id "11" with a field name exists. I'm trying to get specific data from firestore and return it.

Console doesn't show any errors and if I'd return anything else it would print out normally.

Couldn't find any documentations about getting data from firestore to cloud function other than ones that use triggers like: onWrite.

Ad

Answer

Have you tried to make the cloud function async?

exports.getName = functions.https.onCall(async (data, context) => {
    var doc = await db.collection("dogs").doc("{data.id}").get();
    return doc.data().name;
 });
Ad
source: stackoverflow.com
Ad