ReferenceError:节点中未定义next

时间:2020-08-13 10:10:26

标签: javascript node.js backend

在补丁函数中调用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, res, 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, res, 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, could not delete place.",
      500
    );
    return next(error);
  }
  res.status(200).json({ message: "Deleted place." });
};

const patchTodo = async (req, res, 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, could not Update place.",
      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;

1 个答案:

答案 0 :(得分:1)

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

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