Работа с goorm ide и получение ошибки программирования - PullRequest
0 голосов
/ 13 июля 2020
• 1000 пока я не попытался подключиться к серверу mongodb (как всегда), и я не смог пройти. Я дважды и трижды проверил свой код, убедился, что сервер работает в фоновом режиме с помощью команды mongod, и попытался сбросить порт безрезультатно.

Ниже я включил этот код, который я написал (для справки )

var express = require("express"),  
        app = express(),  
 bodyParser = require ("body-parser"), 
   mongoose = require("mongoose") 



mongoose.connect("mongodb://localhost:27017/yelp_camp",{

useNewUrlParser: true,

useCreateIndex: true,

useUnifiedTopology: true });

//SCHEMA SETUP
var campgroundSchema = new mongoose.Schema({
    name: String, 
    image: String, 
    description: String
}); 

var Campground = mongoose.model("Campground", campgroundSchema); 

Campground.create(
    {name: "Briesmann Forest", 
     image: "https://image.shutterstock.com/image-photo/pond-lily-pads-spanaway-lake-260nw-1741738061.jpg",
     description: "This is a huge forest, full of Vampires, Faeries and the occasional witch."
    }, 
    
    function(err, campground){ 
          if (err){
              console.log(err); 
          }  else {
             console.log("NEWLY CREATED CAMPGROUND: ")
             console.log(campground); 
          }
    }); 

app.use(bodyParser.urlencoded({extended: true})); 
app.set("view engine", "ejs"); 

app.get("/", function(req, res){
    res.render("landing"); 
}); 

//INDEX - show all campgrounds
app.get("/campgrounds", function(req, res){
    //Get all Campgrounds from DB
    Campground.find({}, function(err, allCampgrounds){
        if (err){
            console.log(err); 
        } else {
            res.render("index",{campgrounds:allCampgrounds});           
        }
    }); 
}); 
    //res.render("campgrounds",{campgrounds:campgrounds}); 
    
//}); 

app.post("/campgrounds", function(req, res){
         //get data from form and add to campgrounds array
        // redirect to campgrounds page
    var name= req.body.name;
    var image= req.body.image;
    var newCampground= {name: name, image: image}
    //create new campground and save to DB
    Campground.create(newCampground, function(err, newlyCreated){
        if(err){
            console.log("Err")
        } else { 
    //redirect to the campgrounds page
    res.redirect("/campgrounds"); 
} 

    }); 
    
});

//New - Show form to create new campground
app.get("/campgrounds/new", function(req, res){
    res.render("new.ejs"); 
}); 

//SHOW - shows more info about one campgrounds

app.get("/campgrounds/:id", function (req, res){
    //find campground with provided id
    Campground.findById,(req.params.id, function(err, foundCampground){
        if (err){
            console.log (err)
        } else {
        //render show template with that campground 
        res.render ("show.ejs"), {campground:foundCampground}; 

        }
    });  
                    
                         
 app.listen(process.env.PORT || 3000, process.env.IP, function(){
   console.log("The Yelp Camp server has started!")
 }); 
    
}); 
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...