Open giuroperto opened 4 years ago
error when consoling the mongo db connection with x.connections[0].name
How to use ENUM: dishType: { type: String, enum: ['Breakfast', 'Dish', 'Snack', 'Drink', 'Dessert', 'Other'] },
Changed sectors to be: Heavy Industry, Light Industry, Manufacturing, Business, Cottage Industry
to push into a referencced obj Book.update({ _id: req.query.book_id }, { $push: { reviews: { user, comments
Create App
MONGODB_URI='mongodb://localhost/passport-roles'
require('dotenv').config();
"start": "node app.js", "dev": "nodemon app.js"
)const app = express();
app.listen(3000);
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCp_GfKTh7jDVzfNW5aPuUb1RxlG6oNHls"></script>
+ JS
.connect(‘mongodb://localhost/NAMEOFDATABASE’, { useNewUrlParser: true }) .then( x => console.log(
Connected to Mongo! Database name: “${x.connections[0].name}”)) .catch(err => console.error(‘Error connecting to Mongo’, err))
# Dependency directory
node_modules
# Environment Variables should NEVER be published
.env
Create models
module.exports = MODELS
PROPERTY: { type : Schema.Types.ObjectId, ref: 'MODEL' },
Seed the DB
const createAuthors = books.map(book => { Author.create(book.author) .then(author => { return author.name; }) .catch(error => { throw new Error(
Impossible to add the author. ${error}) }) })
let findAuthors = Promise.all(createAuthors) .then(authors => { return books.map(book => { return Author.findOne({name: book.author.name, lastName: book.author.lastName}) .then(author => { if (!author) { throw new Error(
unknown author ${book.author.name} ${book.author.lastName}); } return Object.assign({}, book, {author: author._id}); }) }); }) .catch(error => { throw new Error(error) })
const saveBooks = findAuthors.then(findAuthors => { return Promise.all(findAuthors) .then(books => { return books.map(book => { const newBook = new Book(book); return newBook.save(); }) }) }).then(savedBooks => { Promise.all(savedBooks) .then(books => books.forEach(book => console.log(
created ${book.title}))) .then(() => mongoose.connection.close()) .catch(err => console.log("Error while saving the book: ",err)) })
mongoose.connection.close();
)OTHER CONFIGS
app.js
app.set('view engine', 'hbs'); app.set('views', path.join(__dirname, 'views')); app.use(express.static(path.join(__dirname, 'public')));
if using partialshbs.registerPartials(__dirname + '/views/partials');
app.locals.title = 'Celebrity and Movie Generator';
app.use(cookieParser());
app.use(bodyParser.urlencoded({ extended: true }));
ROUTES
const index = require(‘./routes/index’); app.use(‘/’, index);
.populate('MODELATTRIBUTE')
INDEX ROUTES
OPEN
INTERNAL
USERS
OTHERS
VIEWS
ALL
USER
INTERNAL