-
Notifications
You must be signed in to change notification settings - Fork 1
/
seeds.js
48 lines (45 loc) · 1.48 KB
/
seeds.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
var mongoose = require("mongoose");
var Post = require("./models/post");
var Comment = require("./models/comment");
var data = [
{
name: "Freedom in my mind!",
image: "https://images.unsplash.com/photo-1476527201336-5ec3e1265173?dpr=0.8999999761581421&auto=format&fit=crop&w=767&h=511&q=80&cs=tinysrgb&crop=",
description: "Beautiful cafe racer"
}
];
function seedDb() {
// Remove all posts
Post.remove({}, function (err) {
if (err) {
console.log(err);
}
console.log("removed posts!");
// Add post
data.forEach(function (seed) {
Post.create(seed, function (err, post) {
if (err) {
console.log(err);
} else {
console.log("added post");
// Create a comment
Comment.create(
{
text: "Cool Bike!",
author: "David"
}, function (err, comment) {
if (err) {
console.log(err);
} else {
post.comments.push(comment);
post.save();
console.log("Created new comment");
}
}
);
}
})
});
});
}
module.exports = seedDb;