为什么我云功能继续给一个错误(CloudFirestore与ForOf循环)?

0

的问题

我的功能getDocuments()中的摘要包括在,我通过某些参数的一系列(如道路、名称的文件,如果我要部分通过的部分)的基础上,列我返回的内容的每个文件通过一个循环(ForOf)、功能我做的更多,比任何事情来挽救我太多的行代码,问题是,它总是引发了我一个错误我不知道它是什么。

你能帮帮我吗? 请

云功能

export const employees = functions.https.onRequest((request, response) => {
corsHandler(request, response, async () => {
    return await security.securityLayer(
        { _definedMethod: "GET", userValue: request.method },
        { _definedType: true, _definedLevel: [4], _definedSeconds: 12, userToken: request.header("_token") },
        { required: false },
        { required: false }
    ).then(async (answer) => {
        if (answer.status === 400 || answer.status === 401) {
            return response.status(answer.status).send(answer);
        }

        return await security.getDocuments([
            { collection: "Core/", documentName: "Centers", options: { idReturn: "centros", nestedProperties: [] } },
            {
                collection: "Core/", documentName: "Employees", options: {
                    idReturn: "personal",
                    nestedProperties: [
                          { idReturn: "employees", name: "employee" },
                          { idReturn: "submanager", name: "submanager" },
                          { idReturn: "manager", name: "manager" }
                     ],
                },
            },
        ], SPECIAL_CODE).then((documents) => response.status(documents.status).send(documents))
            .catch(() => response.status(500).send(security.error500(SPECIAL_CODE, 2)));
    }).catch(() => response.status(500).send(security.error500("SPECIAL_CODE", 1)));
});
});

异功能

export async function getDocuments(
documents: {
    collection: string,
    documentName: string,
    options: {
        idReturn: string,
        nestedProperties: {
            idReturn: string,
            name: string
        }[]
    }
}[],
code: string):
Promise<{ status: 201, code: string, subcode: number, devDescription: string, data: any }> {
const data: any = {};
const response: { devDescription: string, subcode: number } = { devDescription: "The document was found and retrieved successfully.", subcode: 1 };

if (documents.length > 1) {
    response.devDescription = "Documents were found and obtained successfully.";
    response.subcode = 2;
}

for (const iterator of documents) {
    const docRef = { path: iterator.collection, name: iterator.documentName };
    const options = { id: iterator.options.idReturn, nestedProperties: iterator.options.nestedProperties };
    const doc = await database.collection(docRef.path).doc(docRef.name).get();

    if (!doc.exists) {
        data[options.id] = "The document " + docRef.name + " does not exist in the specified path: " + docRef.path;

        if (documents.length === 1) {
            response.devDescription = "The document was not found. Check the DATA for more information.";
            response.subcode = 3;
        } else {
            response.devDescription = "One, several or all documents were not found. Check the DATA for more information.";
            response.subcode = 3;
        }
    } else {
        const docData: any = doc.data();
        if (options.nestedProperties.length === 0) {
            data[options.id] = docData;
        } else {
            for (const nested of options.nestedProperties) {
                data[options.id][nested.idReturn] = _.get(docData, nested.name);
            }
        }
    }
}

return { status: 201, code: code, subcode: response.subcode, devDescription: response.devDescription, data: data };
}
firebase javascript node.js typescript
2021-11-23 20:10:26
1

最好的答案

0

我是调查和我看到了,什么是造成的错误是很明显的循环(ForOf),来解决它,我使用的承诺。所有()方法,使实际工作的代码对我来说是以下

export async function getDocuments(
documents: {
    collection: string,
    documentName: string,
    path?: string,
    options: {
        idReturn: string,
        nestedProperties: {
            idReturn: string,
            name: string
        }[]
    }
}[],
code: string):
Promise<{ status: number, code: string, subcode: number, devDescription: string, data: any }> {
const idPrimary: any = Object.values(
    documents.reduce((c: any, v: any) => {
        const k = v.options.idReturn;
        c[k] = c[k] || [];
        c[k].push(v);
        return c;
    }, {})
).reduce((c: any, v: any) => (v.length > 1 ? c.concat(v) : c), []);

if (idPrimary.length > 0) {
    return {
        status: 400, code: code, subcode: 0, data: idPrimary,
        devDescription: "Some return IDs are repeated, check your code and replace the return IDs with unique IDs, for more information see the DATA section." };
}

const response: { devDescription: string, subcode: number } = { devDescription: "The document was found and retrieved successfully.", subcode: 1 };
const queries = [];

if (documents.length > 1) {
    response.devDescription = "Documents were found and obtained successfully.";
    response.subcode = 2;
}

documents.map((document) => {
    if (document.path === undefined) {
        document.path = document.collection + "/" + document.documentName;
    }
});

for (const iterator of documents) {
    queries.push(database.collection(iterator.collection).doc(iterator.documentName).get());
}

return Promise.all(queries).then((snapShot) => {
    const data: any = {};
    snapShot.forEach((doc) => {
        const docProperties = documents.find((item) => item.path === doc.ref.path) ?? null;

        if (!doc.exists) {
            if (docProperties !== null) {
                data[docProperties.options.idReturn] = "The document " + doc.id + " does not exist in the specified path: " + doc.ref.path;

                if (documents.length === 1) {
                    response.devDescription = "The document was not found. Check the DATA for more information.";
                    response.subcode = 3;
                } else {
                    response.devDescription = "One, several or all documents were not found. Check the DATA for more information.";
                    response.subcode = 3;
                }
            }
        } else {
            if (docProperties !== null) {
                const docData: any = doc.data();
                if (docProperties.options.nestedProperties.length === 0) {
                    data[docProperties.options.idReturn] = docData;
                } else {
                    data[docProperties.options.idReturn] = {};
                    for (const nested of docProperties.options.nestedProperties) {
                        if (nested.name === undefined) {
                            data[docProperties.options.idReturn][nested.idReturn] = _.get(docData, nested.idReturn);
                        } else {
                            data[docProperties.options.idReturn][nested.idReturn] = _.get(docData, nested.name);
                        }
                    }
                }
            }
        }
    });
    return { status: 201, code: code, subcode: response.subcode, devDescription: response.devDescription, data: data };
});
}
2021-11-24 16:18:55

其他语言

此页面有其他语言版本

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................