You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React应用中TypeError: Cannot read property 'prototype' of undefined错误的排查与解决

解决React + Express混合项目中的TypeError: Cannot read property 'prototype' of undefined

嘿,我帮你梳理清楚这个问题的核心原因和解决办法了,咱们一步步来:

问题核心原因

你遇到的错误本质是前后端代码没有分离,导致前端浏览器环境试图运行后端专属的Node.js库。

报错里提到的http.ServerResponse.prototype是Node.js内置http模块的内容,但浏览器环境根本没有这个模块,所以它是undefined,当Express试图读取它的prototype时就抛出了错误。而且你把Mongoose(后端数据库工具)也放在了前端项目里,这本身就不符合前后端分离的架构逻辑,直接导致了库的冲突和应用崩溃。

另外你的代码还有个小坑:bookApi.js和Book.js之间出现了循环导入——后端路由导入了Mongoose模型,而模型又反过来导入了路由,这即使架构正确也会引发奇怪的问题,得记得修正。

你的代码问题点

先看这两处明显的问题:

  1. 错误的导入逻辑:在React组件BookCard里导入../bookApi(后端Express路由),前端根本不应该直接引用后端模块,而是通过HTTP请求调用接口。
  2. 循环引用:Book.js里的import Book from '../bookApi'完全没必要,这是把后端路由和Mongoose模型混在一起了,应该删掉这行。

彻底解决方案:前后端代码分离

要彻底解决这个问题,必须把前后端代码拆分成独立的目录,隔离两个运行环境:

1. 重构项目结构

把你的项目改成这样的目录结构:

book-app/
├── client/          # React前端代码
│   ├── src/
│   │   ├── components/
│   │   │   └── BookCard.js
│   │   └── ... 其他前端文件
│   └── package.json
└── server/          # Express后端代码
    ├── models/
    │   └── Book.js  # Mongoose模型
    ├── bookApi.js   # Express路由
    └── package.json

2. 修正后端代码

  • 在server/models/Book.js里,删掉错误的import Book from '../bookApi',只保留Mongoose模型的代码:
const mongoose = require('mongoose');
const BookSchema = new mongoose.Schema({
  nume: { type: String, required: true },
  autor: { type: String, required: true },
  editura: { type: String, required: true },
  poza_coperta: { type: String, required: true },
  gen: { type: String, required: true },
  descriere: { type: String, required: true },
  nota_medie: { type: Number, default: 0 },
  nr_recenzii: { type: Number, default: 0 },
  nr_pagini: { type: Number, required: true },
  data_publicarii: { type: Number, required: true },
  stoc: { type: Number, required: true }
});
module.exports = mongoose.model('book', BookSchema);
  • 在server/bookApi.js里,修正模型的导入路径:
const express = require('express');
const router = express.Router();
const Book = require('./models/Book'); // 改成正确的模型路径

router.get('/:id', (req, res) => {
  Book.findById(req.params.id)
    .then(book => res.json(book))
    .catch(err => res.status(404).json({noBook: 'No book found!'}));
});

module.exports = router;

3. 前端调用后端接口

在BookCard组件里,不要直接导入后端模块,而是用fetch或者Axios发起HTTP请求来调用后端接口,比如:

// BookCard.js里的请求示例
fetch('http://localhost:5000/api/books/123') // 替换成你的后端API地址
  .then(res => res.json())
  .then(book => {
    // 处理book数据
  })
  .catch(err => console.error(err));

4. 分别启动服务

  • 进入client目录,运行npm start启动React开发服务器
  • 进入server目录,运行node bookApi.js(或用nodemon实现热重载)启动Express服务器

这样前后端完全隔离,各自在适合的环境里运行,就不会再出现这类跨环境的错误了。

内容的提问来源于stack exchange,提问作者DannyDSB

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 16:24:08