ReferenceError:节点中未定义next

问题描述

在补丁函数中调用next(error)时出现错误:

(node:1004) UnhandledPromiseRejectionWarning: ReferenceError: next is not defined.

const express = require("express");
const { v4: uuidv4 } = require("uuid");
const Url = require("../models/Url");
const mongoose = require("mongoose");
const HttpError = require("../models/http-errors");

const getTodo = async (req,res,next) => {
  let url;
  try {
    url = await Url.find({});
  } catch (err) {
    const error = new HttpError(
      "Something went wrong,could not find a Url.",500
    );
    return next(error);
  }

  if (!url) {
    const error = new HttpError(
      "Could not find a place for the provided id.",404
    );
    return next(error);
  }

  res.json({
    url: url.map((url) => url.toObject({ getters: true })),});
};

const addTodo = async (req,next) => {
  const { content } = req.body;
  const createdPlace = new Url({
    content: content,enable: false,});
  // Sending it To Mongo
  try {
    await createdPlace.save();
  } catch (err) {
    const error = new HttpError("Creating Place fails mongo");
    return next(error);
  }

  res.sendStatus(200);
};

const deleteTodo = async (req,next) => {
  const { urlId } = req.body;

  let url;
  try {
    url = await Url.findById(urlId);
  } catch (err) {
    const error = new HttpError(
      "Something went wrong,could not delete place.",500
    );
    return next(error);
  }

  if (!url) {
    const error = new HttpError("Could not find Url for this id.",404);
    return next(error);
  }

  //sending it to Mongo
  try {
    const sess = await mongoose.startSession();
    sess.startTransaction();
    await url.remove({ session: sess });
    await sess.commitTransaction();
  } catch (err) {
    const error = new HttpError(
      "Something went wrong,500
    );
    return next(error);
  }
  res.status(200).json({ message: "Deleted place." });
};

const patchTodo = async (req,nect) => {
  const { urlId,enable } = req.body;

  let url;
  try {
    url = await Url.findById(urlId);
  } catch (err) {
    const error = new HttpError(
      "Something went wrong,could not Update place.",500
    );
    return next(error);
  }

  url.enable = enable;
  //sending it to Mongo
  try {
    await url.save();
  } catch (err) {
    const error = new HttpError(
      "Something went wrong,500
    );
    return next(error);
  }

  res.status(200).json({ url: url.toObject({ getters: true }) });
};

exports.getTodo = getTodo;
exports.addTodo = addTodo;
exports.deleteTodo = deleteTodo;
exports.patchTodo = patchTodo;

这是我创建的HttpError。

class HttpError extends Error {
  constructor(message,errorCode) {
    super(message); // Add a "message" property
    this.code = errorCode; // Adds a "code" property
  }
}

module.exports = HttpError;

解决方法

如果您看得很清楚,则说明您拼写了nect,并且使用的是 next (尚不清楚)。

const patchTodo = async (req,res,nect) => {
                                    ^^^

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...