遍历集合中的所有文档时,如何保持函数的生命力?

时间:2019-05-21 12:33:29

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

我在一个集合下大约有100个文档,并且正在尝试检查所有这些文档以检查某个字段是否大于0。如果是,则将其递减(如果不移至下一个文档)。但是,由于状态码200,该功能停止执行。

exports.decrementDaysLeft = functions.https.onRequest((req, res) => {
    res.send(startDecrementing());
});

function startDecrementing() {
    db.collection('PremiumUsers').get().then(snapshot => {
        if (!snapshot.empty) 
        {
            console.log("Query Size " + snapshot.size)
            snapshot.forEach(doc => {
                console.log("Checking for user " + doc.id);
                decrementDaysLeftForUser(doc.id);
            })
            return 200;
        }

    }).catch(err => {
        console.log(err);
        return 400;
    });
};

function decrementDaysLeftForUser(email) {
    var userColl = db.collection('PremiumUsers/' + email + '/subscribedSubjects');
     userColl.get().then(snapshot => {
        if (!snapshot.empty) {
            snapshot.forEach(doc => {
                var num = Number(doc.data().daysLeft);
                if (num > 0) {
                    console.log('Checking ' + doc.id);
                    doc.ref.update({
                        daysLeft: (num - 1).toString()
                    }).then(function () {
                        console.log("Decremented " + doc.id + " for " + email);
                    }).catch(err => {
                        log.console(err);
                    })
                }
                else {
                    console.log('Days left is zero ' + doc.data().daysLeft);
                }
            })
        }
        else {
            console.log('No subjects');
        }
    })
};```

1 个答案:

答案 0 :(得分:0)

响应是在decrementDaysLeftForUser()完成执行之前发送的。在发送响应之前,您需要等待它完成执行。

使用await关键字可确保它在等待功能decrementDaysLeftForUser()完成之前先等待其执行,然后再继续执行当前功能startDecrementing()。注意:await关键字只能在异步函数内使用。该代码将如下所示:

exports.decrementDaysLeft = functions.https.onRequest((req, res) => {
    res.send(startDecrementing());
});

function startDecrementing() {
    db.collection('PremiumUsers').get().then(async (snapshot) => {
        if (!snapshot.empty) 
        {
            console.log("Query Size " + snapshot.size)
            for(let doc of snapshot) {
                console.log("Checking for user " + doc.id);
                await decrementDaysLeftForUser(doc.id);
            }
            return 200;
        }

    }).catch(err => {
        console.log(err);
        return 400;
    });
};

async function decrementDaysLeftForUser(email) {
    var userColl = db.collection('PremiumUsers/' + email + '/subscribedSubjects');
     userColl.get().then(snapshot => {
        if (!snapshot.empty) {
            snapshot.forEach(doc => {
                var num = Number(doc.data().daysLeft);
                if (num > 0) {
                    console.log('Checking ' + doc.id);
                    doc.ref.update({
                        daysLeft: (num - 1).toString()
                    }).then(function () {
                        console.log("Decremented " + doc.id + " for " + email);
                    }).catch(err => {
                        log.console(err);
                    })
                }
                else {
                    console.log('Days left is zero ' + doc.data().daysLeft);
                }
            })
        }
        else {
            console.log('No subjects');
        }
    })
};```

希望这会有所帮助。