【问题标题】:Read data from cloud firestore with firebase cloud function?使用 Firebase 云功能从云 Firestore 读取数据?
【发布时间】:2018-06-30 17:58:38
【问题描述】:

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

重复问题:

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

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

  2. Firebase HTTP Cloud Functions - Read database once

Firebase 文档说:

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

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

我尝试过的:

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

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

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

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

【问题讨论】:

  • 您可以使用Firebase Admin SDK 在 Firestore 中读取和写入数据。还有很多samples。恐怕你必须付出很多努力来学习新事物和研究别人的代码,才能开始让事情按照你想要的方式工作。
  • 感谢@DougStevenson 的快速回复
  • 你发现了吗? @AbhinandanChakraborty
  • @Mans 不,我停止在这个项目中使用 firebase。我学习了 Node.JS,现在我正在完全使用 Node.JS 开发后端。实际上,使用 node 做一些事情并完全控制后端服务器非常容易

标签: android node.js firebase google-cloud-functions google-cloud-firestore


【解决方案1】:

有点晚了,但对于其他偶然发现此问题的人来说。

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

【讨论】:

  • 谢谢您-我敢肯定对某些人很有帮助! :)
  • 它可能会要求字典,所以在 res.send -> 使它成为字典 response.send({"result": stuff })
  • 如何获取我在 index.js 上的方法返回的数据?我需要在我的 TS 文件上这样做,谢谢
  • @Ruan:Firestore + Cloud 功能够稳定吗?我正在考虑将其集成到我的项目中,但我不确定此解决方案的稳定性
  • @HoangTrinh 是的,它们非常稳定。但是,如果您正在寻找 100% 稳定的产品,那么也许可以寻找其他地方。您仍然可以在这里或那里获得重大更新。就像一个月前让我很生气的这个github.com/arnesson/cordova-plugin-firebase/issues/1057
【解决方案2】:

感谢Ruan's answer,这是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);
          });
    });

});

【讨论】:

    猜你喜欢
    • 2018-05-09
    • 1970-01-01
    • 2018-11-22
    • 2018-11-07
    • 2020-08-02
    • 1970-01-01
    • 1970-01-01
    • 2020-07-26
    • 1970-01-01
    相关资源
    最近更新 更多