简体   繁体   English

必须正确处理承诺错误

[英]Promises must be handled appropriately error

I am trying to write this timer based function.我正在尝试编写基于 function 的计时器。 Once a week, it should run and create almost duplicates of existing documents.每周一次,它应该运行并创建现有文档的几乎副本。 Every time I try to launch it, I keep getting the "Promises must be handled appropriately" error, but I can't understand what it is that I am doing wrong?每次尝试启动它时,我都会不断收到“必须正确处理承诺”错误,但我不明白我做错了什么? There's a return at any stage possible.在任何可能的阶段都有回报。 Where is there a promises which I don't satisfy?哪里有我不满足的承诺?

I've previously launched about 40 cloud functions, so I do have some understanding of how promises work, but I'm obviously missing something here, and for the life of me, I can't understand what it is.我之前已经发布了大约 40 个云函数,所以我对 Promise 的工作原理有一些了解,但我显然在这里遗漏了一些东西,而且对于我的生活,我无法理解它是什么。

This is the function:这是 function:

exports.createRecurringDeals = functions.pubsub.schedule('0 0 * * 7').timeZone('Asia/Jerusalem').onRun((context) => {
    db.collection('recurring_deals').get().then(querySnapshot => {

        querySnapshot.forEach(bus => {
            const businessListDoc = bus.data();

            if (businessListDoc !== undefined) {
                const dealsList = businessListDoc.list as Array<String>

                return db.doc('businesses/' + bus.id).get().then(busDoc => {

                    const business = busDoc.data();

                    if (business !== undefined) {

                        dealsList.forEach(deal => {
                            return db.doc('deals/' + deal).get().then(snapshot => {
                                const oldDeal = snapshot.data();
                                if (oldDeal !== undefined) {

                                    const promises: any = [];

                                    const startTime = oldDeal.startTime + oldDeal.interval;
                                    const endTime = oldDeal.endTime + oldDeal.interval;

                                    const newDealDoc = db.collection('deals').doc();

                                    const newDeal = {
                                        id: newDealDoc.id,
                                        business_ID: business.id,
                                        business_name: business.name,
                                        business_address_text: business.address_text,
                                        business_address_lat: business.address_lat,
                                        business_address_long: business.address_long,
                                        business_phone_number: business.phone_number,
                                        business_image: business.restaurant_photos[0],
                                        business_categories: business.categories,
                                        business_sub_categories: business.sub_categories,
                                        discount: oldDeal.discount,
                                        timestamp_start: startTime,
                                        timestamp_end: endTime,
                                        gmt: oldDeal.gmt,
                                        amount: oldDeal.amount,
                                        claimers: [],
                                        active: true
                                    };

                                    promises.push(newDealDoc.set(newDeal));

                                    promises.push(db.doc('recurring_deals/' + business.id).update({ list: FieldValue.arrayRemove(oldDeal.id) }));

                                    promises.push(db.doc('recurring_deals/' + business.id).update({ list: FieldValue.arrayUnion(newDeal.id) }));

                                    return Promise.all(promises);

                                } else {
                                    return null;
                                };
                            });
                        });
                        return null;
                    } else {
                        return null;
                    };
                });
            } else {
                return null;
            };
        });
        return null;
    });
});

I'm guessing that it wants you to add a .catch() clause after the .then in case the Promise fails for whatever reason.我猜它希望你在 .then 之后添加一个.catch()子句.then以防 Promise 由于任何原因而失败。 So for example, try changing例如,尝试改变

return db.doc('deals/' + deal).get().then(snapshot => {
    ...
});

to

return db.doc('deals/' + deal).get().then(snapshot => {
    ...
}).catch(err => console.log(err));

lets start with the notion that every promise has to be handled- either by .catch or .then .让我们从每个 promise必须.catch.then处理的概念开始。 So the first thing you're missing is a handler for the Promise.all() call you have near the end.因此,您缺少的第一件事是您即将结束的Promise.all()调用的处理程序。

Furthermore, in the latest versions of JS, you have to place a catch block after a pomise to handle rejection, even if it does nothing and even if you have a .then .此外,在最新版本的 JS 中,您必须在 pomise 之后放置一个catch块来处理拒绝,即使它什么也不做,即使您有一个.then Here you're missing .catch both for the main promise on top ( db.collection('recurring_deals').get() ), and for the promise.all() call.在这里,您缺少.catch顶部的主要 promise ( db.collection('recurring_deals').get() )和promise.all()调用。

Add those and the error should disappear.添加这些,错误应该消失。

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

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