未处理的“错误”事件 ^ ReferenceError: next is not defined

时间:2021-06-28 20:25:23

标签: javascript node.js reactjs mongodb express

我在尝试编辑特定课程时遇到此错误

<块引用>

events.js:353 扔er; // 未处理的“错误”事件 ^

<块引用>

ReferenceError: next 未定义 在 /Users/mk-elbaz/Downloads/mernnnnnn/routes/course.route.js:36:7 在 /Users/mk-elbaz/Downloads/mernnnnnn/node_modules/mongoose/lib/model.js:4784:16 在模型。查询。 (/Users/mk-elbaz/Downloads/mernnnnnn/node_modules/mongoose/lib/query.js:2119:5) 在model.Query._wrappedThunk [as _findOne] (/Users/mk-elbaz/Downloads/mernnnnnn/node_modules/mongoose/lib/helpers/query/wrapThunk.js:16:8) 在 /Users/mk-elbaz/Downloads/mernnnnnn/node_modules/kareem/index.js:369:33 在 processTicksAndRejections (internal/process/task_queues.js:77:11) 在函数实例上发出“错误”事件: 在 /Users/mk-elbaz/Downloads/mernnnnnn/node_modules/mongoose/lib/model.js:4786:13 在模型。查询。 (/Users/mk-elbaz/Downloads/mernnnnnn/node_modules/mongoose/lib/query.js:2119:5) [...与原始堆栈跟踪匹配的行...] 在 processTicksAndRejections (internal/process/task_queues.js:77:11) npm 错误!代码生命周期 npm 错误!错误号 1 npm 错误! MernJWT@1.0.0 开始:node app.js npm 错误!退出状态 1 npm 错误! npm 错误! MernJWT@1.0.0 启动脚本失败。 npm 错误!这可能不是 npm 的问题。上面可能有额外的日志输出。

course.route.js:

let mongoose = require("mongoose"),
express = require("express"),
router = express.Router();

// Course Model
let courseSchema = require("../models/Course");

// CREATE course
router.route("/create-course").post((req, res, next) => {
  courseSchema.create(req.body, (error, data) => {
    if (error) {
      return next(error);
    } else {
      console.log(data);
      res.json(data);
    }
  });
});

//'new':function(req, res, next)
// READ courses
router.route("/").get((req, res) => {

  courseSchema.find(/*{name : 'CS'},*/(error, data) => {
      if (error) {
        return next(error);
      } else {
        res.json(data);
      }
    });
});

// Get Single course
router.route("/edit-course/:id").get((req, res) => {
  courseSchema.findById(req.params.id, (error, data) => {
    if (error) {
      return next(error);
    } else {
      res.json(data);
    }
  });
});

// Update course
router.route("/update-course/:id").put((req, res, next) => {
  courseSchema.findByIdAndUpdate(
    req.params.id,
    {
      $set: req.body,
    },
    (error, data) => {
      if (error) {
        return next(error);
        console.log(error);
      } else {
        res.json(data);
        console.log("course updated successfully !");
      }
    }
  );
});

// Delete course
router.route("/delete-course/:id").delete((req, res, next) => {
  courseSchema.findByIdAndRemove(req.params.id, (error, data) => {
    if (error) {
      return next(error);
    } else {
      res.status(200).json({
        msg: data,
      });
    }
  });
});

module.exports = router;


app.js:

const express = require("express");
const app = express();
const bodyParser = require("body-parser");
const cookieParser = require("cookie-parser");
const mongoose = require("mongoose");
const userRouter = require("./routes/User.js");
const studentRoute = require("./routes/student.route");
const courseRoute = require("./routes/course.route");
const cors = require("cors");
app.use(cors());
app.use(express.json());
app.use(cookieParser());
app.use("/user", userRouter);
app.use("/courses", courseRoute);
mongoose.connect(
  "mongodb+srv://mkelbaz:nnkoko11@bazdb.0bthm.mongodb.net/myFirstDatabase?retryWrites=true&w=majority",
  { useNewUrlParser: true, useUnifiedTopology: true },
  () => {
    console.log("successfully connected to database");
  }
);

app.use(function(req, res, next) {
    console.log('request', req.url, req.body, req.method);
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, x-token");
    if(req.method === 'OPTIONS') {
        res.end();
    }
    else {
        next();
    }
});

mongoose.Promise = global.Promise;
app.use("/students", studentRoute);

mongoose.set("useFindAndModify", false);

// 404 Error
app.use((req, res, next) => {
  next(createError(404));
});

app.use(function (err, req, res, next) {
  console.error(err.message);
  if (!err.statusCode) err.statusCode = 500;
  res.status(err.statusCode).send(err.message);
});

app.listen(8080, () => {
  console.log("express server started");
});

我该如何解决这个问题?在这种情况下如何定义 next ?我还更改了端口并确保没有其他节点应用程序使用它。

0 个答案:

没有答案