繁体   English   中英

使用 firebase 云 function 从云 Firestore 读取数据?

[英]Read data from cloud firestore with firebase cloud function?

我是一名 Android 开发人员,最近我开始从事一个基于 firebase 云函数和 firestore 数据库的项目。 我正在编写一个 HTTP 触发器 function,它将采用两个参数并将该参数值与 firestore 数据值进行比较,如果该值匹配,则返回 true 或 false 的响应。

重复问题:

是的,已经提出了一些与我相关的问题,但它们并不相似:

  1. Firestore + 云功能:如何从另一个文档中读取

  2. Firebase HTTP Cloud Functions - 一次读取数据库

Firebase 文档说

Cloud Firestore 支持创建、更新、删除和写入事件

我想从 HTTP 触发器中读取 firestore 值。

我试过的:

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

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

我几乎停留在这部分。 任何帮助将不胜感激。 谢谢

PS 我对 JS/TypeScript/NodeJS 的了解非常有限

有点晚了,但对于任何其他绊倒这一点的人来说。

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)
    })
});

感谢阮的回答,这里有一个onCall(..)变体的例子:

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);
          });
    });

});

在 2022 年,我正在尝试以“模块化”的方式来做这件事,就像 firebase 对版本 >= 9 所做的那样。也使用 typescript 作为补充:)。 感谢的灵感。

所以,这就是我的制作方法(类似于以下内容):

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)
    })
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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