简体   繁体   English

Firebase 云功能错误

[英]Firebase cloud functions errors

Greetings of the day to everyone,向大家致以今天的问候,

So I'm having a really hard time with Firebase cause there's just so many versions and things going on.所以我在使用 Firebase 时遇到了很大的困难,因为有太多的版本和正在发生的事情。 Its extremely complicated.它极其复杂。 I wanted to achieve some functionality which is not available through the client modular web 9 version.我想实现一些通过客户端模块化 web 9 版本不可用的功能。

So I have been trying to use the Cloud functions to just get the list of all the collections in a document.所以我一直在尝试使用 Cloud 函数来获取文档中所有集合的列表。

My cloud function looks like this so far -到目前为止,我的云功能看起来像这样 -

const functions = require("firebase-functions");
const admin = require('firebase-admin');
const { object } = require("firebase-functions/v1/storage");
admin.initializeApp();
const db = admin.firestore();

exports.addMessages = functions.https.onCall(async (data, context) => {
    // Grab the text parameter.
    const original = data.text;
    var test;
    const writeResult = admin.firestore().collection('users').doc(original)

    const collections = await writeResult.listCollections();
    collections.forEach(collection => {
        console.log('Found subcollection with id:', collection.id);
        test = collection.id;

    });
    // Send back a message that we've successfully written the message

    return { id: test }
});

I call this function on my front end simply like this --我在前端调用这个函数就像这样——

const functions = getFunctions();
    const addMessage = httpsCallable(functions, 'addMessages');

    const Cloud = () => {
        addMessage({ docPath: `users/${userProfile.uid}` })
            .then(function (result) {
                var collections = result.data.collections;
                console.log(collections);
            })
            .catch(function (error) {
                // Getting the Error details.
                var code = error.code;
                var message = error.message;
                var details = error.details;
                // ...
            });
    }

However, I get a 500 ERROR.但是,我收到 500 错误。 I have checked the payload and everything, the data is being passed and the function is being called but there is some other kind of bug that I seem to be missing.我已经检查了有效负载和所有内容,正在传递数据并正在调用函数,但我似乎还缺少一些其他类型的错误。

If anyone has any ideas please do let me know.如果有人有任何想法,请告诉我。

First off, you're calling addMessage with an Object parameter and then trying to access it's payload by the text field of data , which is undefined.首先,您使用 Object 参数调用addMessage ,然后尝试通过未定义的data text字段访问它的有效负载。 You could either pass users/${userProfile.uid} as a string parameter or assign data.docPath to original .您可以将users/${userProfile.uid}作为字符串参数传递,也可以将data.docPath分配给original

Also, test will only contain the last collection id, since it's being overwritten forEach of the collections.此外, test将仅包含最后一个集合 id,因为它被覆盖forEach集合。 So, may I suggest you make test a string array and push() each collection id to it?那么,我可以建议您test一个字符串数组并将每个集合 id push()送到它吗?

Finally, on the callback of addMessage , there is no data field to access in result .最后,在addMessage的回调中, result中没有数据字段可以访问。 In the case you decide to use an array, result will simply be the array you returned from the cloud function.如果您决定使用数组, result将只是您从云函数返回的数组。

PS I'm not sure I see a reason why this wouldn't work with the V9 modular SDK. PS 我不确定我是否明白为什么这不适用于 V9 模块化 SDK。

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

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