繁体   English   中英

“TypeError: Cannot convert undefined or null to object at Function.entries”使用云功能时出错

[英]"TypeError: Cannot convert undefined or null to object at Function.entries" Error when using Cloud Functions

问题:

我正在使用 Firebase Cloud Functions 从文档中的 Map 检索 boolean,然后根据其值更新同一文档中的时间戳字段。

预期结果:云 Function 更新“Next Date Due”字段并且没有返回任何错误。

实际结果:云 Function 确实按预期更新了“Next Date Due”字段,但在控制台日志中返回以下错误: Unhandled error TypeError: Cannot convert undefined or null to object at Function.entries () at /workspace/ index.js:49:26 在 QuerySnapshot.forEach (/workspace/node_modules/@google-cloud/firestore/build/src/reference.js:807:22) 在 /workspace/index.js:24:37 在 processTicksAndRejections ( internal/process/task_queues.js:95:5) at async /workspace/node_modules/firebase-functions/lib/common/providers/https.js:400:26 (也作为图片附在此处控制台日志错误

我试过的:

我在这里仔细查看了我的实现,但似乎无法弄清楚为什么我会收到此错误,特别是当 Cloud Function 按预期运行良好并在调用时更新值时。

const functions = require("firebase-functions");

const admin = require("firebase-admin");
admin.initializeApp();

 exports.convertNextDate = functions.https.onCall(async (data, context) => {

    var user = context.auth.uid;
    var db = admin.firestore();
    var ref = db.collection("routineInformation").doc(user).collection("personalDailyRoutine");


    var snapshot = await ref.get();


    if (snapshot.empty) {
        console.log('No matching documents.');
        return;
    }


    var result1 = await snapshot.forEach((doc) => {

        console.log("going thru");

        let newOne = doc.data();
        let dateFrequency = newOne['Date Frequency'];
        let repeatingPoints = newOne['Repeating Points'];

        let everyWeek = newOne['Every Week'];
        let twiceWeek = newOne['Twice a Week'];
        let everyMonth = newOne['Every Month'];



        function getDay(numberUsed) {
            const date = new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate());
            const today = date.getDate();
            const dayOfTheWeek = date.getDay();
            const newDate = date.setDate(today - (dayOfTheWeek || numberUsed));
            return new Date(newDate);
        }

        const today = new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate());


          for (let [key, value] of Object.entries(dateFrequency)) {


            if (key == 'Every Day' && value == true) {

                console.log("logging now");

                ref.doc(doc.id).update({
                    "Next Date Due": admin.firestore.Timestamp.fromDate(today)
                });
                return null;
            }

            if (key == 'Every Month' && value == true) {

                const timestamp = everyMonth
                const date = timestamp.toDate();
                after30days = new Date(date.setDate(date.getDate() + 30));

                //rewrite date to 30 days ahead of date stored in repeating points
                ref.doc(doc.id).update({
                    "Next Date Due": admin.firestore.Timestamp.fromDate(after30days)
                });

                //when that day is reached, i.e. == day today, then reset the repeating point date also.
                if (date == today) {
                    ref.doc(doc.id).update({
                        "Repeating Points.Every Month": admin.firestore.Timestamp.fromDate(after30days)
                    });
                }
                return null;

            }

            if (key == 'Every Week' && value == true) {

                console.log("going on")

                for (let [key, value] of Object.entries(everyWeek)) {

                    if (key == 'Monday' && value == true) {
                        ref.doc(doc.id).update({
                            "Next Date Due": admin.firestore.Timestamp.fromDate(getDay(6))
                        });
                    }
                    if (key == 'Tuesday' && value == true) {
                        ref.doc(doc.id).update({
                            "Next Date Due": admin.firestore.Timestamp.fromDate(getDay(5))
                        });
                    }

                    if (key == 'Wednesday' && value == true) {

                        ref.doc(doc.id).update({
                            "Next Date Due": admin.firestore.Timestamp.fromDate(getDay(4))
                        });
                    }

                    if (key == 'Thursday' && value == true) {
                        console.log("retrieving")
                        return ref.doc(doc.id).update({
                            "Next Date Due": admin.firestore.Timestamp.fromDate(getDay(3))
                        });
                    }

                    if (key == 'Friday' && value == true) {
                        ref.doc(doc.id).update({
                            "Next Date Due": admin.firestore.Timestamp.fromDate(getDay(2))
                        });
                    }

                    if (key == 'Saturday' && value == true) {
                        ref.doc(doc.id).update({
                            "Next Date Due": admin.firestore.Timestamp.fromDate(getDay(1))
                        });
                    }

                    if (key == 'Sunday' && value == true) {
                        ref.doc(doc.id).update({
                            "Next Date Due": admin.firestore.Timestamp.fromDate(getDay(0))
                        });
                    }
                    return null;
                }
                return null;
            }

            if (key == 'Twice a Week' && value == true) {

                for (let [key, value] of Object.entries(twiceWeek)) {

                    if (key == 'Monday' && value == true) {

                        if (today.getTime() == getDay(6).getTime()) {
                            ref.doc(doc.id).update({
                                "Next Date Due": admin.firestore.Timestamp.fromDate(getDay(6))
                            });

                        }

                    }
                    if (key == 'Tuesday' && value == true) {

                        if (today.getTime() == getDay(5).getTime()) {
                            ref.doc(doc.id).update({
                                "Next Date Due": admin.firestore.Timestamp.fromDate(getDay(5))
                            });


                        }

                    }
                    if (key == 'Wednesday' && value == true) {

                        if (today.getTime() == getDay(4).getTime()) {
                            ref.doc(doc.id).update({
                                "Next Date Due": admin.firestore.Timestamp.fromDate(getDay(4))
                            });

                        }

                    }
                    if (key == 'Thursday' && value == true) {

                        if (today.getTime() == getDay(3).getTime()) {
                            ref.doc(doc.id).update({
                                "Next Date Due": admin.firestore.Timestamp.fromDate(getDay(3))
                            });

                        }

                    }
                    if (key == 'Friday' && value == true) {

                        if (today.getTime() == getDay(2).getTime()) {
                            ref.doc(doc.id).update({
                                "Next Date Due": admin.firestore.Timestamp.fromDate(getDay(2))
                            });

                        }

                    }
                    if (key == 'Saturday' && value == true) {
                        if (today.getTime() == getDay(1).getTime()) {
                            ref.doc(doc.id).update({
                                "Next Date Due": admin.firestore.Timestamp.fromDate(getDay(1))
                            });

                        }

                    }
                    if (key == 'Sunday' && value == true) {

                        if (today.getTime() == getDay(0).getTime()) {
                            ref.doc(doc.id).update({
                                "Next Date Due": admin.firestore.Timestamp.fromDate(getDay(0))
                            });
                        }

                    }


                }




            }


        })

    })


    return console.log("success");

}

);

为什么我会收到此错误?

我通过将{}放在条件的所有初始部分中解决了这个问题。

因此,对于 Cloud Function 的第一部分:

for (let [key, value] of Object.entries(dateFrequency || {})) {
    if (key == 'Every Day' && value == true) {
        console.log("logging now");

        return  ref.doc(doc.id).update({
            "Next Date Due": admin.firestore.Timestamp.fromDate(today)
        });
        return null;
    }
}

等等。

暂无
暂无

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

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