Javascript 未处理的PromiserEjectionWarning MongoTimeoutError

Javascript 未处理的PromiserEjectionWarning MongoTimeoutError,javascript,node.js,mongodb,Javascript,Node.js,Mongodb,我试图在我的编辑器上本地运行我的app.js,当我尝试在浏览器上运行它时,它在编辑器中工作。当我点击链接进入网站时,它会加载第一个页面,我收到一个未处理的错误PromiserEjectionWarning MongoTimeoutError server selection在30000毫秒后超时。我不熟悉这一点,这实际上是udemy课程的一部分。我在谷歌、reddit线程等网站上搜索过,但似乎无法找到答案。如果这是一个愚蠢的问题,请不要笑。如果你需要更多的代码,让我知道我会提供它 谢谢 这是我的

我试图在我的编辑器上本地运行我的app.js,当我尝试在浏览器上运行它时,它在编辑器中工作。当我点击链接进入网站时,它会加载第一个页面,我收到一个未处理的错误PromiserEjectionWarning MongoTimeoutError server selection在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');
});
Campgrounds.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;

mongo服务器真的启动了吗?你安装了吗?@tramada OMG,对不起,我忘了运行mongod,它现在运行得很好。告诉你们我是新来的,谢谢你们没有让我觉得自己是个十足的白痴。LOL@Tunmee谢谢参考前面的内容comment@ManendarVerma没必要抱歉,mongo服务器真的启动了吗?你安装了吗?@tramada OMG,对不起,我忘了运行mongod,它现在运行得很好。告诉你们我是新来的,谢谢你们没有让我觉得自己是个十足的白痴。LOL@Tunmee谢谢参考前面的内容comment@ManendarVerma没必要道歉