【发布时间】:2023-04-01 04:50:02
【问题描述】:
为什么没有得到结果? sql查询简单正确,在mysql中有效
const getDateBw = (InvoiceDate1, InvoiceDate2, err, req, res) => {
let startDate = new Date(InvoiceDate1);
let endDate = new Date(InvoiceDate2);
let formattedDate1 = JSON.stringify(dateFormat(startDate, "yyyy-mm-dd"));
let formattedDate2 = JSON.stringify(dateFormat(endDate, "yyyy-mm-dd"));
console.log(formattedDate1);
db.query(
"SELECT * FROM dashboard WHERE Invoice_Date BETWEEN " +
formattedDate1 +
" " +
"AND " +
formattedDate2
).then(function (myTableRows) {
res.json({
myTableRows,
});
});
};
getDateBw("2009-05-21", "2021-01-01");
{"level":50,
"time":1598543997608,
"pid":10772,
"hostname":"Ghadi-Mdallal",
"stack":"TypeError: Cannot read property 'json' of undefined\n at C:\\Users\\LENOVO\\Desktop\\Dashboard-V1\\empServer\\handlers\\user.controllers\\user.controllers.js:29:9\n at processTicksAndRejections (internal/process/task_queues.js:97:5)",
"type":"Error",
"msg":"Cannot read property 'json' of undefined"
}
【问题讨论】:
-
您对 getDateBw 的函数调用只有两个参数,而在定义中您也使用 err,res。你不觉得那里缺少什么吗?
-
请通过安慰它来检查 res 的值,因为它看起来像 res 未定义(并且错误表明您正在尝试从未定义的值访问 json 属性)
-
@VipulPatil 如果 res 未定义,我应该通过什么?我记录了它,它是未定义的
-
@AliAsgherBadshah 是的,它是未定义的,我必须怎么做才能定义它?
-
@ghadiMdallal req 和 res 是 express 路由器使用的参数,所以首先从在线教程中访问该部分,例如var router = express.Router(); // 获取一个快速路由器的实例 router.get('/', function(req, res) { // 在这里调用你的查询函数 res.json({ message: 'hooray! welcome to our api!' }) ; });