Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Render mock data #40

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 34 additions & 19 deletions database/db_build.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,38 @@
const career = require('./career_model.js');
const db = require('./db_connection.js');
const careers = require('./seed_data.js');
// const mongoose = require('mongoose');
const mongoose = require('mongoose');

db.once('open', () => {
console.log('we are connected to DB');
career.collection.drop(); // once app is open, drop schema career

careers.forEach((career) => {
career.save((error, result) => {
if (error) {
// render error hbs
return console.log(error);
}
});
var dataLength = 0;
for (var i = 0; i < careers.length; i++) {
careers[i].save((error, result) => {
Copy link
Collaborator

@matthewdking matthewdking Oct 3, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did some research and found out that save is a wrapper for insert and update, see this. If this is true then we could get rid of the for loop and use insertMany() instead.

if (error) {
console.log(error);
} else {
dataLength++;
console.log('adding to db');
if (dataLength === careers.length) {
exit();
console.log('done');
} // no else
}
});
});

// not sure if we need this
// function exit () {
}
function exit () {
mongoose.disconnect();
}
//
//
// db.once('open', () => {
// console.log('we are connected to DB');
// Career.collection.drop(); // once app is open, drop schema career
//
// careers.forEach((career) => {
// career.save((error, result) => {
// if (error) {
// return console.log(error);
// }
// console.log(result);
// console.log('add one schema to DB');
// });
// });
// mongoose.disconnect();
// }
// });
3 changes: 2 additions & 1 deletion database/db_connection.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const mongoose = require('mongoose');
require('env2')('./congif.env');
require('env2')('./config.env');

const db = mongoose.connection;
mongoose.Promise = global.Promise;
mongoose.connect(process.env.MONGODB_URI, {
useMongoClient: true
});
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"devDependencies": {
"chai": "^4.1.2",
"codecov": "^2.3.0",
"env2": "^2.2.0",
"mocha": "^4.0.0",
"nodemon": "^1.12.1",
"nyc": "^11.2.1",
Expand Down
12 changes: 11 additions & 1 deletion src/controllers/career.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
const Career = require('./../../database/career_model.js');

module.exports = (req, res) => {
res.render('career');
Career.find({}, (error, result) => {
if (error) {
console.log('Error with rendering data', error);
} else {
res.render('career', { career: result });
console.log('this is the result:', result);
console.log();
}
});
};
8 changes: 4 additions & 4 deletions src/views/career.hbs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<section class="career-card">

<h1>We are the amazing GERMS!</h1>
<h2 class="career-title">{{ career.title }}</h2>
<image class="career-icon" src="{{ career.icon }}" alt="{{ career.title }}">
<p class="career-tagline"> {{ career.tagline }}</p>
<h2 class="career-title">{{ career[0].title }}</h2>
<image class="career-icon" src="{{ this.icon }}" alt="{{ this.title }}">
<p class="career-tagline"> {{ this.tagline }}</p>

</section>