即使结果正确,函数也不会在ref.on()中响应

时间:2019-11-19 13:33:54

标签: javascript php node.js firebase firebase-realtime-database

我一直在尝试创建一个名为工作区的应用程序。之前我曾问过另一个问题,但现在添加的功能更多。有一个备注系统。我尝试使用不同版本的代码,并且给出的代码具有我创建的最佳版本。我在网上找不到我的问题的答案,因此我不得不在这里提问。

var ref = firebase.database().ref(); 

function stdRemarks(studentName){
    let finalStuff;
    ref.on("value", function(snapshot){
        let keys = Object.keys(snapshot.val().schools[returnCurrentUser()][studentName]['remarks']);
        for(i=0;i<keys.length;i++){
            let objectToDealWith = snapshot.val().schools[returnCurrentUser()][studentName]['remarks'];
            let remark = objectToDealWith[keys[i]]['remark'];
            let examiner = objectToDealWith[keys[i]]['examiner'];
            let fullRemark = ` ${examiner}: ${remark} | `
            finalStuff +=  fullRemark;
        }
        return finalStuff;
    });
}

ref.on("value", function(snapshot){
     let dashTab = document.getElementById("dashboard_table");
     let btn = document.getElementById("csv_blob");
     let btn2 = document.getElementById("json_view");
     let btn3 = document.getElementById("json_export");
     btn.style.display = "block";
     btn2.style.display = "block";
     $("#json_export").css('display', 'block');
     dashTab.innerHTML = "<thead><tr><th>Student Name</th><th>Class</th><th>Email</th><th>Subject</th><th>Project Info</th><th>Remarks</th><th>Project</th><th style='display: none;'>Project Download URL</th><th>Add Remark</th></tr></thead>";
     let jsonRecieved = snapshot.val();
     let objectToDealWith = snapshot.val().schools[returnCurrentUser()];
     let lengthOfIt = Object.size(objectToDealWith);
     for(i=0;i<lengthOfIt;i++){
        let int = i + 1;
        let names = Object.keys(objectToDealWith);
        let stdName = names[i];
        let finalResult = objectToDealWith[stdName];
        document.getElementById("schoolnameis").innerText = "Dashboard - " + objectToDealWith['i'];
        let stdClass = finalResult['class'];
        let stdEmail = finalResult['email'];
        let stdSubject = finalResult['subject'];
        let stdiName = finalResult['stdname'];
        let stdProjectName = finalResult['projectname']
        let stdProjectInfo = finalResult['projectinfo'];
        let stdProjectLink = finalResult['projectlink'];
        console.log(stdRemarks(stdiName))
        let elementToPush = `<tr><td>${stdiName.replace(/undefined/g, '')}</td><td>${stdClass.replace(/undefined/g, '')}</td><td>${stdEmail.replace(/undefined/g, '')}</td><td>${stdSubject.replace(/undefined/g, '')}</td><td>${stdProjectInfo.replace(/undefined/g, '')}</td><td>${stdRemarks(stdnameName).replace(/undefined/g, '')}</td><td><a href=${stdProjectLink}>${stdProjectName.replace(/undefined/g, '')}</a></td><td style='display:none;'>${stdProjectLink}</td><td id="${stdName}" style='text-align:center;' onclick="closeThatSomeThing();getIdOfTd(this.id)">&#x2795</td></tr>`;
        dashTab.innerHTML += elementToPush;
     }
});

所以一切正常,但是这里的一些东西似乎破坏了整个代码。我的数据库看起来像这样 Have look

这是错误。

//A warning by firebase.
@firebase/database: FIREBASE WARNING: Exception was thrown by user callback. TypeError: Cannot read property 'replace' of undefined

//An error occuring on the variable `elementToPush` and its part ${stdRemarks(stdnameName).replace(/undefined/g, '') in the code.
Cannot read property 'replace' of undefined

我必须明天提交这个项目。

2 个答案:

答案 0 :(得分:0)

您的函数“ stdRemarks”的返回类型为“ void”。返回完整的ref.on()或将'finalstuff'移动到.on()函数调用之外,并确保函数'stdRemarks'具有所需的返回类型。在这种情况下,这将是一个“字符串”;

function stdRemarks(studentName){
    let finalStuff = "";
    ref.on("value", function(snapshot){
        let keys = Object.keys(snapshot.val().schools[returnCurrentUser()][studentName]['remarks']);
        for(i=0;i<keys.length;i++){
            let objectToDealWith = snapshot.val().schools[returnCurrentUser()][studentName]['remarks'];
            let remark = objectToDealWith[keys[i]]['remark'];
            let examiner = objectToDealWith[keys[i]]['examiner'];
            let fullRemark = ` ${examiner}: ${remark} | `
            finalStuff +=  fullRemark;
        }
    });
     return finalStuff;
}

答案 1 :(得分:0)

我通过使用此代码使其工作。

ref.on("value", function(snapshot){
     let dashTab = document.getElementById("dashboard_table");
     let btn = document.getElementById("csv_blob");
     let btn2 = document.getElementById("json_view");
     let btn3 = document.getElementById("json_export");
     btn.style.display = "block";
     btn2.style.display = "block";
     $("#json_export").css('display', 'block');
     dashTab.innerHTML = "<thead><tr><th>Student Name</th><th>Class</th><th>Email</th><th>Subject</th><th>Project Info</th><th>Remarks</th><th>Project</th><th style='display: none;'>Project Download URL</th><th>Add Remark</th></tr></thead>";
     let jsonRecieved = snapshot.val();
     let objectToDealWith = snapshot.val().schools[returnCurrentUser()];
     let lengthOfIt = Object.size(objectToDealWith)-1;
     for(i=0;i<lengthOfIt;i++){
        let finalRemark;
        let int = i + 1;
        let names = Object.keys(objectToDealWith);
        let stdName = names[i];
        let finalResult = objectToDealWith[stdName];
        let stdClass = finalResult['class'];
        let stdEmail = finalResult['email'];
        let stdSubject = finalResult['subject'];
        let stdiName = finalResult['stdname'];
        for(var e=0;e<Object.size(objectToDealWith[stdName]['remarks']);e++){
            let keys = Object.keys(objectToDealWith[stdName]['remarks']);

            let remark = objectToDealWith[stdName]['remarks'][keys[e]]['remark'];
            let examiner =  objectToDealWith[stdName]['remarks'][keys[e]]['examiner'];
            let completeRemark = ` | ${examiner} : ${remark}  `
            finalRemark += completeRemark;
        }

        let stdProjectName = finalResult['projectname']
        let stdProjectInfo = finalResult['projectinfo'];
        let stdProjectLink = finalResult['projectlink'];
        let elementToPush = `<tr><td>${stdiName}</td><td>${stdClass}</td><td>${stdEmail}</td><td>${stdSubject}</td><td>${stdProjectInfo}</td><td>${finalRemark.replace(/undefined/g, '')}</td><td><a href=${stdProjectLink}>${stdProjectName}</a></td><td style='display:none;'>${stdProjectLink}</td><td id="${stdName}" style='text-align:center;' onclick="closeThatSomeThing();getIdOfTd(this.id)">&#x2795</td></tr>`;
        dashTab.innerHTML += elementToPush;
     }
});

我在这里所做的是,我在第二个循环中将我转到了e,并且有效...