不能使用"undefined"作为Firestore值 [英] Cannot use “undefined” as a Firestore value

查看:59
本文介绍了不能使用"undefined"作为Firestore值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用node.js从我的Firestore数据库中检索数据,我想从一个Firestore查询中收集一个字段,并将该值传递给另一个Firestore查询,但是我一直在日志中得到此错误,这是第一个Firestore查询成功检索数据,但我的问题是将值传递给第二个查询

I am trying to retrieve data from my Firestore database using node.js, I want to collect a field from one Firestore query and pass the value into another Firestore query but I keep getting this error in my logs, the first Firestore query successfully retrieves data, but my problem is passing a value to the second query

Error: Value for argument "value" is not a valid query constraint. Cannot use "undefined" as a Firestore value. If you want to ignore undefined values, enable `ignoreUndefinedProperties`. at Object.validateUserInput (/workspace/node_modules/@google-cloud/firestore/build/src/serializer.js:271:19) at validateQueryValue (/workspace/node_modules/@google-cloud/firestore/build/src/reference.js:2048:18) at CollectionReference.where (/workspace/node_modules/@google-cloud/firestore/build/src/reference.js:988:9) at step2 (/workspace/index.js:74:43) at /workspace/index.js:65:17 at QuerySnapshot.forEach (/workspace/node_modules/@google-cloud/firestore/build/src/reference.js:748:22) at updateBets (/workspace/index.js:60:22) at processTicksAndRejections (internal/process/task_queues.js:97:5)

这是我的代码

async function updateBets() {

    var marketRef = db.collection('matches');
    var snapshot = await marketRef.where('matchStatus', '==', 'FINISHED').get();

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

    console.log('I found documents');

    snapshot.forEach(doc => {
        step2();

        async function step2() {
            var marketRef2 = db.collection('markets');
            var snapshot2 = await marketRef2.where('marketId', '==', doc.data().matchId).get();

            console.log(doc2.id, '=>', doc2.data());

            snapshot2.forEach(doc2 => {
                console.log(doc2.id, '=>', doc2.data());

                if (doc2.data().marketTitleId == 'FULL_TIME_RESULT') {

                    var a = doc.data().homeTeamScore;
                    var b = doc.data().awayTeamScore;

                    var winnerIndex;

                    if (a > b) {
                        winnerIndex = 0;

                        var resultIndex = ['WINNER', 'LOSER', 'LOSER'];
                        var docName = `${doc.data().matchId}` + '000' + '1';

                        var sfRef = db.collection('markets').doc(docName);
                        batch5.update(sfRef, {
                            results: resultIndex
                        });

                    } else if (a == b) {
                        winnerIndex = 1;

                        var docName = `${doc.data().matchId}` + '000' + '1';

                        var resultIndex = ['LOSER', 'WINNER', 'LOSER'];

                        var sfRef = db.collection('markets').doc(docName);
                        batch5.update(sfRef, {
                            results: resultIndex
                        });
                    } else if (a < b) {
                        winnerIndex = 2;

                        var docName = `${doc.data().matchId}` + '000' + '1';

                        var resultIndex = ['LOSER', 'LOSER', 'WINNER'];

                        var sfRef = db.collection('markets').doc(docName);
                        batch5.update(sfRef, {
                            results: resultIndex
                        });
                    }
                }
            })
        }
    });

    batch5.commit().then(() => {
        console.log("im done with results");
    }).catch((err) => {
        console.log('Mac! there was an error with results: ', err);
    });
}

推荐答案

您可以尝试:const data = doc.data();const matchId = data.matchId;然后将matchId放入查询中.

You could try: const data = doc.data(); const matchId = data.matchId; and then put matchId into query.

还要记录"matchId"变量以查看值.

Also log the "matchId" variable to see the value.

这篇关于不能使用"undefined"作为Firestore值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆