【问题标题】:Cannot read property 'json' of undefined无法读取未定义的属性“json”
【发布时间】: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!' }) ; });

标签: mysql node.js


【解决方案1】:

您没有在 getDateBw("2009-05-21", "2021-01-01"); 中传递“res”;

res.json 如何在里面工作?它会给出“无法读取未定义的属性'json'”错误。

【讨论】:

  • res 如果不通过则未定义。
  • 我已将 res 作为参数发送,但仍未定义
  • 我已将 res 作为参数发送,但仍未定义
  • 您在哪个函数中调用 getDateBw ?您需要传递 Express 或 Node.js 或任何其他 FW 为您提供的“res”。那个“res”里面有.json。仅仅通过任何“res”是行不通的。
猜你喜欢
  • 1970-01-01
  • 2019-11-13
  • 2020-12-28
  • 2015-10-06
  • 2023-03-20
  • 1970-01-01
  • 1970-01-01
  • 2023-03-23
  • 1970-01-01
相关资源
最近更新 更多