开发者问题收集

TypeError:无法将未定义或空转换为对象 - 尝试在路由器中执行 MySQL 查询时

2021-12-19
3211

我有一个执行几个 MySQL 查询的路由 - 最后一个查询是 SELECT,使用路由中执行的上一个查询中的“电子邮件”值作为条件。我使用 MySQL Workbench 测试了其中一些“电子邮件”值并得到了结果,所以我很确定我的 JS 代码是错误的。不确定这是否需要采用 async/await 方法,但感觉可能是这样 - 我只是不确定从哪里开始。

这是错误:

在此处输入图像描述

以及引发错误(330)的行:

在此处输入图像描述

这是路线的完整代码:

// ROUND III: GETTING EVENTS AND ENROLLMENTS BY EMAIL FROM eLearningRecords TABLE; Then will need to create a SELECT query joining enrollmentrefresh and tb_elr_prereqs on emial. 
// All learndot enrollments GET route .......................................................................................................
// .................................................................................................................................
router.get("/elearning_enrollments", (err, res) => {
    // STEP 3 - DELETE EXISTING DATA FROM THE tb_elr_prereqs table ===========================================================
    // create the DELETE query and set to variable
    var sql_delete_events_and_enrollments = `DELETE FROM tb_elr_prereqs;`

    // execute the DELETE query
    pool2.query(sql_delete_events_and_enrollments, (err, results, fields) => {
        if (err) {
            console.log("Failed to delete records from tb_elr_prereqs!!!")
            console.log(err)
            res.sendStatus(500)
            return
        }
        console.log("Deleted the existing data from the tb_elr_prereqs table");
        // res.end()
    
    // END (STEP 3) DELETE QUERY SECTION ===========================================================================================

    // =============================================================================================================================

    // STEP 4 - GET EVENTS AND ENROLLMENTS FROM contact, enrollment, and location tables ===========================================

    // create a function
    function get_elearningrecords(){
    var sql_getemails = `SELECT email FROM enrollmentrefresh;`
    
    // execute the query
    pool.query(sql_getemails, (err, rows, results)=>{
        console.log("getting the emails from enrollmentrefresh table")
        console.log("sql_getemails" + Object.keys(rows).length)
        for(i=1;i <= Object.keys(rows).length; i++){
            if(rows[i] != undefined){
                for(var i in rows){
                    var email = JSON.stringify(rows[i].email)
                    var email = email.replace('"\\"', '')
                    var email = email.replace('\\""', '')
                    console.log(email)
                    var sql_getelr = 
                        `SELECT
                            registrationID,
                            courseName,
                            email
                        FROM
                            eLearningRecords
                        WHERE
                            email = "${email}"
                        (
                            SCORMLESSONSTATUS LIKE 'passed'
                            OR 
                            registrationstatus LIKE 'PASSED'
                        )
                        AND
                        (
                            (courseName LIKE '%Fundamentals%' AND courseName LIKE '%Part 3%')
                            OR 
                            courseName LIKE '%Creating Dashboards%'
                            OR 
                            courseName LIKE '%Advanced Searching%'
                            OR 
                            courseName LIKE '%Core Consultant Labs%'
                        );`

                    console.log(sql_getelr);
                    // execute the SELECT query
                    pool2.query(sql_getelr, (err, rows, results)=>{
                        console.log("sql_getelr" + Object.keys(rows).length)
                    })
                }
            }
        }
    })
}

    get_elearningrecords();

    })
})
2个回答

您的基本问题在于问题出在这里

WHERE
        email = "${email}"
    (
        SCORMLESSONSTATUS LIKE 'passed'
        OR 
        registrationstatus LIKE 'PASSED'
    )

这不是有效的 sql 代码。

这应该是

WHERE
  email = "${email}"
AND
    (
        SCORMLESSONSTATUS LIKE 'passed'
        OR 
        registrationstatus LIKE 'PASSED'
    )

此外

${email}

这看起来更像是 php 代码而不是 javascript

查看 防止 Node.js 中的 SQL 注入 如何在 javascript 中传递数据

nbk
2021-12-19

问题出现在第 294 行。数据库的响应始终以数组 ( rows ) 的形式出现。

您尝试在数组上使用 Object 方法,但结果却引发错误 无法将未定义或 null 转换为对象 。您应该在数组上使用数组方法。

将以下几行更改为:

第 294 行: console.log("sql_getemails" + rows.length)

第 295 行: for(i=1;i <= rows.length; i++)

第 297 行: rows.forEach(i=> {//your code goes here})

其余部分看起来还行,但并不好。您的代码陷入了回调地狱。为了逃避回调地狱,我建议在你的函数和路由上使用 async/await

jkalandarov
2022-01-06