Good day, Please help me with this code cannot sleep with this im trying to solve the problem but i can't i repeated the code for a couple of times but the error "TypeError: Cannot read property 'push' of undefined Node.js express" in the command line is still showing the same error.
this is the all error message in the command line.
C:\Users\User\Desktop\YelpCamp\v3>node app YelpCamp Server is now Serving ... remove campground added a campground added a campground added a campground C:\Users\User\Desktop\YelpCamp\v3\node_modules\mongoose\node_modules\mongodb\lib\utils.js:123 process.nextTick(function() { throw err; }); ^
TypeError: Cannot read property 'push' of undefined at C:\Users\User\Desktop\YelpCamp\v3\seeds.js:47:30 at Function. (C:\Users\User\Desktop\YelpCamp\v3\node_modules\mongoose\lib\model.js:3753:16) at C:\Users\User\Desktop\YelpCamp\v3\node_modules\mongoose\lib\model.js:1939:14 at C:\Users\User\Desktop\YelpCamp\v3\node_modules\async\internal\parallel.js:35:9 at C:\Users\User\Desktop\YelpCamp\v3\node_modules\async\internal\once.js:12:16 at iteratorCallback (C:\Users\User\Desktop\YelpCamp\v3\node_modules\async\eachOf.js:52:13) at C:\Users\User\Desktop\YelpCamp\v3\node_modules\async\internal\onlyOnce.js:12:16 at C:\Users\User\Desktop\YelpCamp\v3\node_modules\async\internal\parallel.js:32:13 at apply (C:\Users\User\Desktop\YelpCamp\v3\node_modules\lodash_apply.js:15:25) at C:\Users\User\Desktop\YelpCamp\v3\node_modules\lodash_overRest.js:32:12 at model.callbackWrapper (C:\Users\User\Desktop\YelpCamp\v3\node_modules\mongoose\lib\model.js:1902:11) at next_ (C:\Users\User\Desktop\YelpCamp\v3\node_modules\hooks-fixed\hooks.js:89:34) at fnWrapper (C:\Users\User\Desktop\YelpCamp\v3\node_modules\hooks-fixed\hooks.js:186:8) at C:\Users\User\Desktop\YelpCamp\v3\node_modules\mongoose\lib\model.js:3753:16 at C:\Users\User\Desktop\YelpCamp\v3\node_modules\mongoose\lib\model.js:266:5 at C:\Users\User\Desktop\YelpCamp\v3\node_modules\mongoose\lib\model.js:153:7
C:\Users\User\Desktop\YelpCamp\v3>
comment.js file
var mongoose = require("mongoose");
var commentSchema = mongoose.Schema({
text:String,
author:String
});
module.exports = mongoose.model("Comment",commentSchema);
seeds.js
var mongoose = require("mongoose");
var Campground = require("./models/campground");
var Comment = require("./models/comment");
var data = [
{
name:"Crimson",
image:"http://www.co.jefferson.id.us/use_images/Parks/pavedsite.jpg",
description: "blah blah blah"
},
{
name:"Yumi",
image:"http://www.co.jefferson.id.us/use_images/Parks/pavedsite.jpg",
description: "blah blah blah"
},
{
name:"Tine",
image:"http://www.co.jefferson.id.us/use_images/Parks/pavedsite.jpg",
description: "blah blah blah"
},
];
function seedDB() {
//remove all campgrounds
Campground.remove({},function(err) {
if(err) {
console.log(err);
}
console.log("remove campground");
//add a few campgrounds
data.forEach(function(seed) {
Campground.create(seed,function(err,campground) {
if(err){
console.log(err);
}else {
console.log("added a campground");
//add a few comments
Comment.create(
{
text:"This place is greatest",
author:"homer"
}, function(err,comment){
if(err) {
console.log(err);
}else {
campground.comments.push(comment); //THIS IS THE ERROR im trying to solve
campground.save();
console.log("Craeted new Comments");
}
});
}
});
});
});
}
module.exports = seedDB;
app.js routing file
var express = require("express"),
bodyParser = require("body-parser"),
mongoose = require("mongoose"),
app = express(),
Campground = require("./models/campground");
var seedDB = require("./seeds");
app.set("view engine","ejs");
app.use(bodyParser.urlencoded({extended:true}));
mongoose.connect("mongodb://localhost/yelp_camp");
seedDB();
app.get("/",function(req,res) {
res.render("landing");
});
//(ROUT)INDEX - SHOW ALL CAMP GROUNDS
app.get("/campgrounds",function(req,res) {
//GET ALL CAMPGROUNDS IN DB
Campground.find({},function(err,allCampgrounds){
if(err){
console.log(err);
}else {
res.render('index',{campgrounds:allCampgrounds});
}
});
});
//NEW - SHOW FORM TO CREATE CAMPGROUND
app.get("/campgrounds/new",function(req,res) {
res.render("new.ejs");
});
//CREATE - ADD NEW CAMPGROUNDS TO DB
app.post("/campgrounds",function(req,res) {
var name = req.body.name;
var image = req.body.image;
var desc = req.body.description;
var newCampground = {name: name,image: image, description: desc};
//CREATE NEW CAMPGROUND AND SAVE TO DATABASE
Campground.create(newCampground,function(err,newlyCreated) {
if(err) {
console.log(err);
}else {
//REDIRECT BACK TO CAMPGROUND!
res.redirect("/campgrounds");
}
});
});
//SHOW - SHOWS MORE INFO ABOUT ONE CAMPGROUND
app.get("/campgrounds/:id",function(req,res) {
//FIND THE CAMPGROUND WITH THE PROVIDED ID
Campground.findById(req.params.id,function(err,foundCampground) {
if(err) {
console.log(err);
}else {
res.render('show',{campground:foundCampground});
}
console.log(req);
});
});
app.listen(3000, function() {
console.log("YelpCamp Server is now Serving ... ");
});
via Crimson Tiangco
No comments:
Post a Comment