TypeError:无法将未定义或空值转换为对象 - 尝试在路由器中执行 MySQL 查询时
Posted
技术标签:
【中文标题】TypeError:无法将未定义或空值转换为对象 - 尝试在路由器中执行 MySQL 查询时【英文标题】:TypeError: Cannot convert undefined or null to object - when trying to execute MySQL query in router 【发布时间】:2022-01-21 12:40:41 【问题描述】:我有一个执行几个 mysql 查询的路由 - 最终查询是一个 SELECT,它使用路由中执行的前一个查询中的“电子邮件”值作为条件。我已经使用 MySQL Workbench 测试了其中一些“电子邮件”值并得到了结果,所以我很确定我的 JS 代码是错误的。不确定这是否需要采用异步/等待方法,但感觉可能是这样 - 我只是不确定从哪里开始。
这是错误:
以及抛出错误的行(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();
)
)
【问题讨论】:
【参考方案1】:你的基本问题在于这里有问题
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
看看Preventing SQL injection in Node.js如何在javascript中传递数据
【讨论】:
【参考方案2】:问题出在第 294 行。来自数据库的响应总是以数组的形式出现(rows
)。
您正在尝试在引发错误Cannot convert undefined or null to object
的数组上使用 Object 方法。你应该在数组上使用数组方法。
将以下几行更改为:
第 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
。
【讨论】:
以上是关于TypeError:无法将未定义或空值转换为对象 - 尝试在路由器中执行 MySQL 查询时的主要内容,如果未能解决你的问题,请参考以下文章
在“reactstrap”导航栏中使用“折叠”时“无法将未定义或空值转换为对象”
收到错误“创建 HelloWorld 错误:TypeError:无法将未定义或 null 转换为对象”
Firebase 函数 - 发送推送并收到错误消息“TypeError:无法将未定义或 null 转换为对象”[关闭]