【问题标题】:Getting firestore data from a Google Cloud Function with array-contains-any使用 array-contains-any 从 Google Cloud 函数获取 firestore 数据
【发布时间】:2021-07-27 01:49:53
【问题描述】:

网页调用 Firebase 函数,该函数应从 Firestore 集合中获取多条记录,一切运行无误,但不返回任何数据。当我在页面中直接(不是通过函数)运行查询时,会返回正确的数据。

在网页中:

var getUserConfigFiles = firebase.functions().httpsCallable('getUserConfigFiles');
        
getUserConfigFiles()
    .then((result) => {
         console.log("Yay - Firebase result ===>",result);
    })
    .catch((error) => {
        console.warn("error",error.code,error.message,error.details)
    });

在 index.js 中:

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

const cors = require('cors')({origin: true});
const db = admin.firestore();

然后是函数本身:

exports.getUserConfigFiles = functions.https.onCall((data, context) => {
    if (!context.auth) {
        return {"status": "error", "code": 499, "message": "The function must be called while authenticated"};
    }
    
    const uid = context.auth.uid;
    const email = context.auth.token.email;

    var outcome = {"status": "OK", "code": 200, "requestor": uid, "email": email, "configfiles":[]};
    
    // search on either the user's userid or their email
    outcome.searcharray = [uid];
    if (email) {
        outcome.searcharray.push(email);
    }
    
    return db.collection("configfiles").where("memberAttached", "array-contains-any", outcome.searcharray)
        .get()
            .then((querySnapshot) => {
                querySnapshot.forEach((doc) => {
                    outcome.configfiles.push({"tokenid": doc.id, "data-all": doc.data()});
                })

                // next row is returning the object just to see if it's there
                outcome.querySnapshot = querySnapshot;
                // 

                return (outcome);
            })
            .catch((error) => {
                return ({"status": "error", "code": 402, "message": error,"requestor": uid});
            });
    
 });

一切正常,结果返回一个查询快照。除了没有数据,.configfiles 应该有一百左右的行。如果我只在网页中运行db.collection("configfiles").where("memberAttached... 部分,则会返回数据。

我已经搜索并尝试了许多方法,但我显然缺少一些基本的东西。有人可以帮忙吗?

【问题讨论】:

    标签: javascript firebase google-cloud-firestore google-cloud-functions


    【解决方案1】:

    我怀疑 outcome.querySnapshot = querySnapshot 行会导致问题,因为 querySnapshot 不是 JSON 对象,因此无法返回。我建议删除该行,然后重试。

    如果这不能解决问题,您能否添加一些日志记录,看看代码是否曾到达then 内部?

    【讨论】:

    • 这让我走上了正轨!我在 Firebase 控制台中看不到任何 cmets,所以我去寻找它们。基本问题是模拟器正在运行这些功能,但数据在firestore中。当我关闭函数模拟器时,一切正常。
    • 很高兴听到你认为我们的@AndrewNZ ?
    猜你喜欢
    • 2020-04-04
    • 2020-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-01
    • 1970-01-01
    • 2019-09-30
    相关资源
    最近更新 更多