[英]ReferenceError: expressValidator is not defined Express , ejs, Nodejs
[英]nodejs express ReferenceError: next is not defined
我正在嘗試控制我從帶有 joi 驗證的表單中獲取的輸入。 function validateCampgrount
是一個用於檢查它的中間件,但是當我嘗試編寫下一個 function 時,它說 next is not defined.*
我在哪里使用next()
const validateCampground = (req, res, next) => {
const { error } = campgroundSchema.validate(req.body);
if (error) {
const msg = error.details.map((el) => el.message).join(",");
throw new ExpressError(msg, 400);
} else {
next();
}
};
錯誤信息
next();
^
ReferenceError: next is not defined
我在哪里使用 function
app.post(
"/campgrounds",
validateCampground(
catchAsync(async (req, res, next) => {
// if(!req.body.campground) throw new ExpressError('Invalid Data', 400)
const campground = new Campground(req.body.campground);
await campground.save();
res.redirect(`/campgrounds/${campground._id}`);
})
)
);
看來您使用的中間件有誤。 有關完整指南,請參閱本教程。
在您的代碼中,您將 function 包裝在中間件中。 相反,您需要將中間件作為參數傳遞給app.post()
。 像這樣:
app.post("/campgrounds", validateCampground, async (req, res, next) => {
const campground = new Campground(req.body.campground);
await campground.save();
res.redirect(`/campgrounds/${campground._id}`);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.