Skip to content

Instantly share code, notes, and snippets.

@meesont
Last active March 25, 2019 12:01
Show Gist options
  • Save meesont/c07c0afd280ecf1d335fdd51a735eb3a to your computer and use it in GitHub Desktop.
Save meesont/c07c0afd280ecf1d335fdd51a735eb3a to your computer and use it in GitHub Desktop.
Better version of Seeds.js for Colt Steele's Web Developer Bootcamp - Better error handling, also seeds comments.
var mongoose = require("mongoose"),
Campground = require("./models/campground"),
Comment = require("./models/comment");
var data = [
{
name: "Cloud's Rest",
image: "https://farm4.staticflickr.com/3795/10131087094_c1c0a1c859.jpg",
description: "blah blah blah"
},
{
name: "Desert Mesa",
image: "https://www.backpackerguide.nz/wp-content/uploads/2016/01/free-campsites-in-the-north-island.jpg",
description: "blah blah blah"
},
{
name: "Canyon Floor",
image: "https://farm1.staticflickr.com/189/493046463_841a18169e.jpg",
description: "blah blah blah"
}
];
function seedDB(){
Campground.deleteMany({}, function(err){
if(err){
console.log(err);
} else {
console.log('removed campgrounds');
Comment.deleteMany({}, function(err){
if(err){
console.log(err);
} else {
console.log('removed comments');
data.forEach(function(seed) {
Campground.create(seed, function(err, campground){
if(err) {
console.log(err);
} else {
console.log('added campground');
Comment.create({
text: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean luctus pellentesque lectus, in accumsan sapien tempor non. Nulla faucibus cursus convallis. Proin interdum augue bibendum varius lacinia.',
author: 'Homer'
}, function(err, comment){
if(err) {
console.log(err);
} else {
campground.comments.push(comment);
campground.save(function(err){
if(err){
console.log(err);
} else {
console.log('created new comment');
}
});
}
});
}
});
});
}
});
}
});
}
module.exports = seedDB;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment