简体   繁体   中英

Read data from cloud firestore with firebase cloud function?

I'm an Android developer and recently I've started working on a project based on firebase cloud functions and firestore database. I'm writing an HTTP trigger function that will take two parameters and compare that parameter value with the firestore data value and if the value matches then return a response of true or else false.

Duplicate Question:

Yes, there are some question already asked related to mine but they are not similar:

  1. Firestore + cloud functions: How to read from another document

  2. Firebase HTTP Cloud Functions - Read database once

Firebase Docs says :

Cloud Firestore supports create, update, delete, and write events

I want to read firestore value from HTTP trigger.

What I have tried:

exports.userData = functions.https.onRequest((req, res) => {

 const user = req.query.user;
 const pass = req.query.pass;
});

I'm pretty much stuck at this part. Any help will be greatly appreciated. Thanks

PS I have very limited knowledge related to JS/TypeScript/NodeJS

a bit late, but for any one else stumbling upon this.

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


exports.someMethod = functions.https.onRequest((req, res) => {
    var stuff = [];
    var db = admin.firestore();
    db.collection("Users").doc("7vFjDJ63DmhcQiEHwl0M7hfL3Kt1").collection("blabla").get().then(snapshot => {

        snapshot.forEach(doc => {
            var newelement = {
                "id": doc.id,
                "xxxx": doc.data().xxx,
                "yyy": doc.data().yyy
            }
            stuff = stuff.concat(newelement);
        });
        res.send(stuff)
        return "";
    }).catch(reason => {
        res.send(reason)
    })
});

Thanks to Ruan's answer , here's an example for onCall(..) variation:

exports.fireGetColors = functions.https.onCall((data, context) => {

    return new Promise((resolve, reject) => {

        var colors = {};

        var db = admin.firestore();
        db.collection('colors')
          .get()
          .then(snapshot => {

              snapshot.forEach(doc => {
                  var key = doc.id;
                  var color = doc.data();
                  color['key'] = key;

                  colors[key] = color;
              });

              var colorsStr = JSON.stringify(colors, null, '\t');
              console.log('colors callback result : ' + colorsStr);

              resolve(colors);
          })
          .catch(reason => {
              console.log('db.collection("colors").get gets err, reason: ' + reason);
              reject(reason);
          });
    });

});

In 2022, I am trying to do this thing in "Modular" way as what firebase has for version >= 9. Using typescript too as an addition:). Thanks to Ruan for the inspiration.

So, here is how I made it ( similar to the following ):

import * as functions from "firebase-functions";
import { getFirestore } from "firebase-admin/firestore";
import { initializeApp } from "firebase-admin/app";
initializeApp(functions.config().firebase);


export const someMethod = functions.https.onRequest((req, res) => {
    let stuff: any[] = [];
    let db = getFirestore();
    db.collection("Users").doc("7vFjDJ63DmhcQiEHwl0M7hfL3Kt1").collection("blabla").get().then(snapshot => {

        snapshot.forEach(doc => {
            var newelement = {
                "id": doc.id,
                "xxxx": doc.data().xxx,
                "yyy": doc.data().yyy
            }
            stuff = stuff.concat(newelement);
        });
        res.send(stuff)
        return "";
    }).catch(reason => {
        res.send(reason)
    })
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM