Skip to content

Commit

Permalink
import post and user
Browse files Browse the repository at this point in the history
  • Loading branch information
georgina puig committed Jul 17, 2020
1 parent 8b4a7a6 commit c222d19
Showing 1 changed file with 10 additions and 28 deletions.
38 changes: 10 additions & 28 deletions references.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const mongoose = require('mongoose'); // npm install mongoose --save
const Post = require('./models/post');
const User = require('./models/user');

// mongoose setup
mongoose.connect('mongodb://localhost:27017/blog_demo_2', {
Expand All @@ -8,33 +10,13 @@ mongoose.connect('mongodb://localhost:27017/blog_demo_2', {
.then(() => console.log('Connected to DB!'))
.catch(error => console.log(error.message));

// POST - title, content
const postSchema = new mongoose.Schema({
title: String,
content: String
});
const Post = mongoose.model("Post", postSchema);

// USER - email, name
const userSchema = new mongoose.Schema({
email: String,
name: String,
posts: [
{
type: mongoose.Schema.Types.ObjectId,
ref: 'Post'
}
]
});
const User = mongoose.model("User", userSchema);

// User.create({
// email: 'bob@gmail.com',
// name: 'Bob Belcher'
// });

// Post.create({
// title: 'How to cook the best burguer pt. 3',
// title: 'How to cook the best burguer pt. 4',
// content: 'lalalala alallala'
// }, function(err, post) {
// User.findOne({email: 'bob@gmail.com'}, function(err, foundUser) {
Expand All @@ -55,10 +37,10 @@ const User = mongoose.model("User", userSchema);

// find user
// find all post for that user
User.findOne({email: 'bob@gmail.com'}).populate('posts').exec(function(err, user) {
if (err) {
console.log(err);
} else {
console.log(user);
}
});
// User.findOne({email: 'bob@gmail.com'}).populate('posts').exec(function(err, user) {
// if (err) {
// console.log(err);
// } else {
// console.log(user);
// }
// });

0 comments on commit c222d19

Please sign in to comment.