Jerry Chen
Jerry Chen

Reputation: 13805

Retrieve Data from Firestore by Using Cloud Functions HTTP Trigger

My code is working fine until db.collection("OrderId").doc("D9XjS3efiV12epxQcgYA").get().then since it returns "firestoreFunc runnig" when I uncomment the line, but not logging and returns nothing inside db.collection("OrderId").doc("D9XjS3efiV12epxQcgYA").get().then.

How can I get access to Firestore using HTTP trigger?

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

exports.firestoreFunc = functions.https.onCall((data, context) =>  {
    //return "firestoreFunc running";
    db.collection("OrderId").doc("D9XjS3efiV12epxQcgYA").get().then(snapshot =>  {

        console.log("log : 22");

        return 22;
    }).catch(reason =>  {

    })
});

Upvotes: 1

Views: 1703

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317467

Your function has to return a promise that resolves with the data you want to send to the client. As shown now, your function returns nothing, and the document fetch will probably not complete, as the function will be terminated because it doesn't know to wait for the fetch.

Just add a return from the promise returned by get():

return db.collection("OrderId").doc("D9XjS3efiV12epxQcgYA").get().then(snapshot =>  {
    console.log("log : 22");
    return 22;
}).catch(reason =>  {
    // you should handle errors here
})

Upvotes: 1

Related Questions