javascript - UnhandledPromiseRejectionWarning MongoTimeoutError
问题描述
我正在尝试在我的编辑器上本地运行我的 app.js,当我尝试在浏览器上运行它时它在编辑器中工作,当我单击链接进入站点时它会加载第一页,我收到错误 UnhandledPromiseRejectionWarning MongoTimeoutError服务器选择在 30000 毫秒后超时。我是新手,这实际上是 udemy 课程的一部分。我在谷歌、reddit 线程等上进行了搜索。似乎无法弄清楚这一点。如果这是一个愚蠢的问题,请不要笑。如果您需要更多代码,请告诉我,我会提供。
谢谢
这是我的 app.js
var express = require("express"),
app = express(),
bodyParser = require("body-parser"),
mongoose = require("mongoose"),
flash = require("connect-flash"),
passport = require("passport"),
LocalStrategy = require("passport-local"),
methodOverride = require("method-override"),
Campground = require("./models/campground"),
Comment = require("./models/comment"),
User = require("./models/user")
// seedDB = require("./seeds")
// mongoose.Promise = global.Promise
//requiring routes
var commentRoutes = require("./routes/comments"),
campgroundRoutes = require("./routes/campgrounds"),
indexRoutes = require("./routes/index")
mongoose.connect("mongodb://localhost/yelp_camp_", {
useNewUrlParser: true,
useUnifiedTopology: true,
})
app.use(bodyParser.urlencoded({extended: true}));
app.set("view engine", "ejs");
app.use(express.static(__dirname + "/public"));
app.use(methodOverride("_method"));
app.use(flash());
//seedDB(); //seed the database
//PASSPORT CONFIGURATION
app.use(require("express-session")({
secret: "Bodybuilding is fun",
resave: false,
saveUninitialized: false
}));
app.use(passport.initialize());
app.use(passport.session());
passport.use(new LocalStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
app.use(function(req, res, next){
res.locals.currentUser = req.user;
res.locals.error = req.flash("error")
res.locals.success = req.flash("success")
next();
});
app.use("/", indexRoutes);
app.use("/campgrounds", campgroundRoutes);
app.use("/campgrounds/:id/comments", commentRoutes);
app.listen(3000, function (){
console.log('The YelpCamp Server Has Started');
});
露营地.js
var express = require("express");
var router = express.Router();
var Campground = require("../models/campground");
var middleware = require("../middleware");
//INDEX - show all campgrounds
router.get("/", function(req, res){
// Get all campgrounds from DB
Campground.find({}, function(err, allCampgrounds){
if(err){
console.log(err);
} else {
res.render("campgrounds/index",{campgrounds:allCampgrounds});
}
});
});
//CREATE - add new campground to DB
router.post("/", middleware.isLoggedIn, function(req, res){
// get data from form and add to campgrounds array
var name = req.body.name;
var price = req.body.price;
var image = req.body.image;
var desc = req.body.description;
var author = {
id: req.user._id,
username: req.user.username
}
var newCampground = {name: name, price: price, image: image, description: desc, author:author}
// Create a new campground and save to DB
Campground.create(newCampground, function(err, newlyCreated){
if(err){
console.log(err);
} else {
//redirect back to campgrounds page
console.log(newlyCreated);
res.redirect("/campgrounds");
}
});
});
//NEW - show form to create new campground
router.get("/new", middleware.isLoggedIn, function(req, res){
res.render("campgrounds/new");
});
// SHOW - shows more info about one campground
router.get("/:id", function(req, res){
//find the campground with provided ID
Campground.findById(req.params.id).populate("comments").exec(function(err, foundCampground){
if(err || !foundCampground){
req.flash("error", "Campground not found");
res.redirect("back");
} else {
console.log(foundCampground);
//render show template with that campground
res.render("campgrounds/show", {campground: foundCampground});
}
});
});
//EDIT CAMPGROUND ROUTE
router.get("/:id/edit", middleware.checkCampgroundOwnership, function(req, res){
Campground.findById(req.params.id, function(err, foundCampground){
res.render("campgrounds/edit", {campground: foundCampground});
});
});
//UPDATE CAMPGROUND ROUTE
router.put("/:id", middleware.checkCampgroundOwnership, function(req, res){
//find and update the correct campground
Campground.findByIdAndUpdate(req.params.id, req.body.campground, function(err, updatedCampground){
if(err){
res.redirect("/campgrounds");
} else {
res.redirect("/campgrounds/" + req.params.id);
}
});
//redirect somewhere(show page)
});
//DESTROY CAMPGROUND ROUTE
router.delete("/:id", middleware.checkCampgroundOwnership, function(req, res){
Campground.findByIdAndRemove(req.params.id, function(err){
if(err){
res.redirect("/campgrounds");
} else
res.redirect("/campgrounds");
});
});
//middleware
function isLoggedIn(req, res, next){
if(req.isAuthenticated()){
return next();
}
res.redirect("/login");
}
module.exports = router;
解决方案
推荐阅读
- graphviz - 点中两条记录的排名相同
- python - 与python中子进程打开的`/dev/tty`交互
- c - 如何在 C 中为自定义 Wireshark 解剖器创建新名称?
- json - 通过 Power Automate 创建专用通道 JSON 和/或图形 API 有什么问题?
- python - MySQL 执行语句返回 NoneType
- javascript - 无法使用 vuejs 和 electronjs 创建简单的文件资源管理器
- python - IndexError:索引 2 超出轴 2 的范围,大小为 2
- vue.js - 从其他组件调用方法
- angular - 如何根据显示值过滤 agGrid 中的行?
- perforce - 我们如何在 perforce 中签出文件的特定版本?