我一直在嘗試使用 try 和 catch 在異步函式上實作 Express 中的錯誤處理以及創建一個單獨的檔案,但是當我得到錯誤 express 無法處理它時,該站點就會崩潰。
我已經在 app.get('/campgrounds/:id... 上測驗了它...我會連接到一個假 id 以查看錯誤處理程式是否捕獲它但它沒有。
JS檔案:
const express = require('express');
const path = require('path');
const mongoose = require('mongoose');
const Campground = require('./modules/campground');
const engine = require('ejs-mate');
const methodOverride = require('method-override');
const cities = require('./seeds/cities')
const ExpressError = require('./utilities/ExpressError');
const catchAsync = require('./utilities/catchAsync');
mongoose.connect('mongodb://localhost:27017/yelpcamp');
const db = mongoose.connection;
db.on("error", console.error.bind(console, "connection error:"));
db.once("open", () => {
console.log("Database connected");
});
const app = express();
app.engine('ejs', engine);
app.set('view engine','ejs');
app.set('views',path.join(__dirname,'views'));
app.use(express.urlencoded({extended:true}));
app.use(methodOverride('_method'));
app.get('/',(req,res)=>{
res.redirect('/campgrounds');
})
app.get('/campgrounds',catchAsync(async (req,res)=>{
const campgrounds = await Campground.find({});
res.render('campgrounds/index',{campgrounds})
}))
app.get('/campgrounds/new',catchAsync(async (req,res)=>{
const campgrounds = await Campground.find({});
res.render('campgrounds/new',{ campgrounds })
}))
app.post('/campgrounds',catchAsync(async(req,res, next)=>{
const newCampground = new Campground(req.body);
await newCampground.save();
res.redirect(`campgrounds/${newCampground._id}`);
}))
app.patch('/campgrounds/:id', catchAsync(async (req,res)=>{
const {id}=req.params;
const campground = await Campground.findByIdAndUpdate(id, req.body, {runValidators: true, new: true});
res.redirect(`/campgrounds/${id}`)
}))
app.delete('/campgrounds/:id',catchAsync(async (req,res)=>{
const { id } = req.params;
await Campground.findByIdAndDelete(id);
res.redirect('/campgrounds');
}))
app.get('/campgrounds/:id',catchAsync(async (req,res,next)=>{
const { id } = req.params;
const campground = await Campground.findById(id);
res.render('campgrounds/details',{campground})
}))
app.get('/campgrounds/:id/edit',catchAsync(async(req,res)=>{
const { id } = req.params;
const campgrounds = await Campground.find({});
const campground = await Campground.findById(id);
res.render('campgrounds/edit',{campground, campgrounds});
}))
app.use((err,req,res,next)=>{
res.send('Error here!');
})
app.listen(3000,()=>{
console.log('Listening on port 3000')
})
這是名為 catchAsync 的單獨檔案:
module.exports = func =>{
return (req,res,next) =>{
func(req,res,next).catch(next);
}
}
uj5u.com熱心網友回復:
null如果Mongoose沒有找到使用findById. 所以在你的情況下const campground = await Campground.findById(id);不會拋出錯誤/拒絕,而是null傳遞campground給你的 ejs-template,這可能會導致你描述的錯誤。
如果你想真正拋出一個錯誤并強制呼叫你的錯誤處理程式,你可以從你的/find-handler拋出一個錯誤,例如
app.get('/campgrounds/:id',catchAsync(async (req,res,next)=>{
const { id } = req.params;
const campground = await Campground.findById(id);
if(!campground) {
throw new Error("Campground not found");
}
res.render('campgrounds/details',{campground})
}))
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/357790.html
標籤:javascript 节点.js MongoDB 表达 异步
上一篇:如何修復我的洗掉按鈕?Reactjs、MondogDB和Nodejs
下一篇:獲取架構內請求陣列中的資料
