【问题标题】:How to await multiple functions with thousands of promises Firebase Functions如何使用数千个 Promise 等待多个函数 Firebase Functions
【发布时间】:2020-11-09 06:26:33
【问题描述】:

我每晚都使用下面的代码删除所有旧数据。

每个功能都可以正常工作,如果我创建多个计划任务,每个功能一个,它也可以正常工作。但是,当我将它们组合成一个名为 scheduleCleanData 的计划任务时,我收到 错误:无法加载默认凭据。浏览至https://cloud.google.com/docs/authentication/getting-started 了解更多信息。 在 GoogleAuth.getApplicationDefaultAsync (/workspace/node_modules/google-auth-library/build/src/auth/googleauth.js:161:19) 在 process._tickCallback (internal/process/next_tick.js:68:7)

根据this post,我相信这是由于函数没有等待回调,而不是凭据问题。但是,添加 asyncawait 关键字会导致解析错误。其中一些集合需要删除数千条记录。

任何帮助如何修改此代码以正确等待将不胜感激!

const functions = require('firebase-functions');
const admin = require('firebase-admin');
const client = require('firebase-tools');
admin.initializeApp(functions.config().firebase)
const db = admin.firestore();

const runtimeOpts = {
    timeoutSeconds: 300,
    memory: '1GB'
}

exports.scheduledCleanData = functions
    .runWith(runtimeOpts)
    .pubsub.schedule('0 3 * * *')
    .timeZone('America/Chicago')
    .onRun((context) => {
        return cleanOldAssignments()
            .then(cleanOldDuties())
            .then(cleanOldEvents())
            .then(console.info("scheduledCleanData Complete!"));
    });

function cleanOldAssignments() {
    const dateYesterday = new Date(new Date().getTime() - (24 * 60 * 60 * 1000));   // 24 hours
    return db.collectionGroup('assignments').where('date', '<', dateYesterday).get()
        .then(querySnapshot => {
            console.info("Old assignments to remove: " + querySnapshot.size);
            const promises = [];
            querySnapshot.forEach(doc => {
                promises.push(doc.ref.delete());
            });
            return Promise.all(promises);
        });
}

function cleanOldDuties() {
    const dateYesterday = new Date(new Date().getTime() - (24 * 60 * 60 * 1000));   // 24 hours
    return db.collectionGroup('duties').where('date', '<', dateYesterday).get()
        .then(querySnapshot => {
            console.info("Old duties to remove: " + querySnapshot.size);
            const promises = [];
            querySnapshot.forEach(doc => {
                promises.push(doc.ref.delete());
            });
            return Promise.all(promises);
        });
}

function cleanOldEvents() {
    const dateYesterday = new Date(new Date().getTime() - (24 * 60 * 60 * 1000));   // 24 hours
    return db.collectionGroup('events').where('date', '<', dateYesterday).get()
        .then(querySnapshot => {
            console.info("Old events to remove: " + querySnapshot.size);
            const promises = [];
            querySnapshot.forEach(doc => {
                promises.push(doc.ref.delete());
            });
            return Promise.all(promises);
        });
}

【问题讨论】:

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


    【解决方案1】:

    如果 cleanOldDutiescleanOldEvents 都返回更多的承诺,你的代码没有考虑到它们。您将需要显式返回它们的值以沿承诺链传播:

        return cleanOldAssignments()
            .then(() => { return cleanOldDuties() })
            .then(() => { return cleanOldEvents() })
    

    在没有看到这些其他函数的确切代码的情况下,不确定这是否会起作用。这些功能也必须正确实现。

    async/await 几乎总是一种更清晰的方式来表达这些事情。您可能也只是错误地使用了它们。

    【讨论】:

    • 您好,Doug,感谢您的留言。 cleanOldDuties 和 cleanOldEvents 的代码也显示在上面。所有代码都在那里。我已经调整了代码,但仍然得到错误:无法加载默认凭据。浏览至cloud.google.com/docs/authentication/getting-started 了解更多信息。在 GoogleAuth.getApplicationDefaultAsync (/workspace/node_modules/google-auth-library/build/src/auth/googleauth.js:161:19) 在 process._tickCallback (internal/process/next_tick.js:68:7)
    猜你喜欢
    • 2019-01-15
    • 2017-01-15
    • 2019-09-17
    • 1970-01-01
    • 2021-01-09
    • 1970-01-01
    • 2020-06-26
    • 2016-12-18
    • 2021-12-29
    相关资源
    最近更新 更多