如何解决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) => {
^^^
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。