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

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日

悬赏问题

  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教