TypeError 的解决方案:Cannot read property 'prototype' of undefined in React App

时间:2021-05-09 11:04:37

标签: javascript node.js reactjs express typeerror

我对所有暗示 JavaScript 的东西都不熟悉,我不理解错误。我记录了自己一点点,有人说错误可能是由编写错误的函数或类似的东西引起的。

这是我的课

<块引用>

bookApi.js

const express = require('express');
const router = express.Router();

const Book = require('./Screens/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;
<块引用>

Book.js

import Book from '../bookApi';
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 = Book = mongoose.model('book', BookSchema);

我也有这样使用它的类:

const BookCard = (props) => {
    const book = props.book;

import Book from '../bookApi';

最后一个是唯一的导入方法,至少在cmd提示符下编译代码没有给我错误,但是出现'TypeError',我不明白原因。

我尝试将此示例改编为我的项目:https://blog.logrocket.com/mern-stack-tutorial/

错误说明:

TypeError: Cannot read property 'prototype' of undefined
(anonymous function)
D:/Torrents/Other-Files/Team Project/test/test1/node_modules/express/lib/response.js:42
  39 |  * @public
  40 |  */
  41 | 
> 42 | var res = Object.create(http.ServerResponse.prototype)
  43 | 
  44 | /**
  45 |  * Module exports.
View compiled

./node_modules/express/lib/response.js

http://localhost:3000/static/js/vendors~main.chunk.js:83006:30
__webpack_require__
D:/Torrents/Other-Files/Team Project/test/test1/webpack/bootstrap:856
  853 | 
  854 | __webpack_require__.$Refresh$.init();
  855 | try {
> 856 |     modules[moduleId].call(module.exports, module, module.exports, hotCreateRequire(moduleId));
      | ^  857 | } finally {
  858 |     __webpack_require__.$Refresh$.cleanup(moduleId);
  859 | }

编辑:我的错误是我在我的反应应用程序(前端)中编写了后端代码,正如朋友告诉我的那样,这会使库相互“打架”,从而导致应用程序崩溃。解决方案:将后端与前端分开。为您的 React 应用程序创建一个独立的目录,您将只保留前端,并在文件夹中的另一个目录中创建一个您只保留后端的 react 应用程序目录。例如app/client 代表前端,app/server 代表后端。

0 个答案:

没有答案