简体   繁体   English

Firebase未捕获的TypeError:无法读取未定义的属性'forEach'

[英]Firebase Uncaught TypeError: Cannot read property 'forEach' of undefined

I'm trying to count the number of misbehavior on the two routes I've made in my database. 我正在尝试计算我在数据库中所做的两条路线上的不当行为的数量。 Below are the structure of my firebase database under drivers and reports database respectively: 以下是我的firebase数据库的结构,分别位于驱动程序和报告数据库下:

[drivers database] - i.stack.imgur.com/Q6GKs.png [驱动程序数据库]-i.stack.imgur.com/Q6GKs.png

[reports database] - i.stack.imgur.com/ALWPu.png [报告数据库]-i.stack.imgur.com/ALWPu.png

Here's my counter for counting the number of misbehavior: 这是我用来计算不当行为次数的计数器:

        <script>
        var route1Count = 0;
        var route2Count = 0;
        var drivers;
        var reports;

        var driversRef = firebase.database().ref('drivers/');
        var reportsRef = firebase.database().ref('reports/');

        driversRef.once('value', (snapshot) => {
            drivers = snapshot;
        });

        reportsRef.once('value', (snapshot) => {
            reports = snapshot;
        });

        drivers.forEach((driver) => {

            var violationCount = reports.filter((report) => report.val().plateNumber === driver.key).length;

            if(driver.val().route === "Fairview - Quiapo"){
                route1Count += violationCount;
            }else if(driver.val().route === "Quiapo - Fairview"){
                route2Count += violationCount;
            }
        });

        document.getElementById("demo").innerHTML = "route1: " + route1Count + "route2: " + route2Count;

    </script>

I get this error message: Uncaught TypeError: Cannot read property 'forEach' of undefined at drivers.forEach, all inputs will be greatly appreciated! 我收到此错误消息:Uncaught TypeError:无法读取drivers.forEach中未定义的属性“ forEach”,所有输入将不胜感激! Thanks! 谢谢!

Error Message : 错误信息 : https://i.stack.imgur.com/W5iGZ.png

you could nest them, or if you run this in an environment that supports es6's Promise object (which your code suggests), you could use the once() returning a promise and more elegantly do: 您可以嵌套它们,或者如果您在支持es6的Promise对象(您的代码建议)的环境中运行它,则可以使用Once once()返回一个promise,并且更优雅地做到这一点:

Promise.all([driversRef.once('value'), reportsRef.once('value')])
  .then(([driversSnapshot, reportsSnapshot]) => {
    // ...
  })

暂无
暂无

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

相关问题 未捕获的类型错误:无法读取未定义的属性“forEach”//检测未定义 - Uncaught TypeError: Cannot read property 'forEach' of undefined// detect undefined 未捕获(承诺)TypeError:无法读取未定义的属性“ forEach” - Uncaught (in promise) TypeError: Cannot read property 'forEach' of undefined 未捕获的TypeError:无法读取未定义的ReactJS的属性&#39;forEach&#39; - Uncaught TypeError: Cannot read property 'forEach' of undefined ReactJS 未捕获的类型错误:无法读取未定义解决方案的属性“classList/forEach”? - Uncaught TypeError: Cannot read property 'classList/forEach' of undefined solution? React&Firebase-未捕获的TypeError:无法读取未定义的属性&#39;setState&#39; - React & firebase- uncaught TypeError: Cannot read property 'setState' of undefined Firebase recaptchaVerifier 显示未捕获的类型错误:无法读取未定义的属性“RecaptchaVerifier” - Firebase recaptchaVerifier showing Uncaught TypeError: Cannot read property 'RecaptchaVerifier' of undefined 未捕获的类型错误:无法读取 null 的属性“forEach” - Uncaught TypeError: Cannot read property 'forEach' of null 未捕获的TypeError:无法读取未定义的属性“未定义” - Uncaught TypeError: Cannot read property 'undefined' of undefined Firebase-未捕获(承诺)TypeError:无法读取属性 - Firebase - Uncaught (in promise) TypeError: Cannot read property Firebase:未捕获的TypeError:无法读取属性&#39;signInWithEmailAndPassword&#39; - Firebase: Uncaught TypeError: Cannot read property 'signInWithEmailAndPassword'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM