首页 文章

使用firebase Cloud 功能从 Cloud 端防火墙读取数据?

提问于
浏览
2

我是Android开发人员,最近我开始研究基于firebase Cloud 功能和firestore数据库的项目 . 我正在编写一个HTTP触发器函数,它将接受两个参数并将该参数值与firestore数据值进行比较,如果值匹配,则返回true或其他错误的响应 .

Duplicate Question:

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

Firebase Docs says

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

我想从HTTP触发器读取firestore值 .

What I have tried:

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

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

我几乎坚持这一部分 . 任何帮助将不胜感激 . 谢谢

附:我对JS / TypeScript / NodeJS的知识非常有限

2 回答

  • 6

    有点晚了,但对于其他任何一个绊脚石的人来说 .

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

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

相关问题