郭小胖~ 2022-01-20 13:21 采纳率: 71.4%
浏览 262
已结题

TypeError: Cannot read properties of undefined (reading 'hander')

问题遇到的现象和发生背景

就是不太懂这个test已经是对象了。hander是方法但是调用不了 一调用就报错

const http = require("http");
const url = require("url");
let routers = [];
class Application {
  get(path, hander) {
    routers.push({
      path,
      method: "get",
      hander,
    });
  }
  listen() {
    const server = http.createServer(function (req, res) {
      const { pathname } = url.parse(req.url); //在路径中解析出来pathname
      // console.log(pathname);
      // console.log(req.method.toLowerCase());
      //在路由表routers通过pathname,找到回调,然后执行
      var test = routers.find((v) => {
        // console.log(v);
        // console.log(v.path);
        // console.log(v.method);
           return v.path == pathname && req.method.toLowerCase() == v.method;
      });
       console.log(test);
      // console.log(test.hander);
      // console.log(test.path);
      // var re = test.hander(req, res)
      // console.log(re);
        return test.hander(req,res)
      
    });
    //在Application原型上添加listen方法匹配路径,执行对应的hander
    server.listen(...arguments);
  }
}
module.exports = function () {
  return new Application();

问题相关代码,请勿粘贴截图
运行结果及报错内容

img

我的解答思路和尝试过的方法
我想要达到的结果
  • 写回答

1条回答 默认 最新

  • 於黾 2022-01-20 13:32
    关注

    你的函数hander在哪定义呢
    就是这句吗
    routers.push({
    path,
    method: "get",
    hander,
    });
    可是它的实现在哪呢,调用它执行什么代码呢

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 1月28日
  • 已采纳回答 1月20日
  • 创建了问题 1月20日

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站