Good afternoon guys. I've been working on building a little contact list app and I wanted to use mongoose for storing data and session items. I've never used mongoose before so I picked up a quick PDF to follow along with. Most of if makes sense and I was able to piece some stuff together and get authentication running to add new users using a UserSchema. Works great. What I'm a little lost on now is how can I use mongoose to create a second schema called ContactSchema. I thought I would create 2 different files for each schema (user.js and contact.js), but when I try that, I still don't see a contact collection when I run show collections in my mongo terminal and I also don't throw any errors. It's also the same when I check for it in robomongo.
My server file calls the all my models like this:
require('./server/models').connect(config.dbUri);
// config.dbUri -> "dbUri": "mongodb://localhost/react_app"
My user.js file:
const mongoose = require('mongoose');
const bcrypt = require('bcrypt');
// define the User model schema
const UserSchema = new mongoose.Schema({
email: {
type: String,
index: { unique: true }
},
password: String,
name: String
});
...bcrypt code here
module.exports = mongoose.model('User', UserSchema);
My contact.js file:
const mongoose = require('mongoose');
// define the Contact model schema
const ContactSchema = new mongoose.Schema({
content : String,
updated_at : Date
});
module.exports = mongoose.model('Todo', ContactSchema);
My index.js file to require both user.js and contact.js schemas:
const mongoose = require('mongoose');
module.exports.connect = (uri) => {
mongoose.connect(uri);
// plug in the promise library
mongoose.Promise = global.Promise;
mongoose.connection.on('error', (err) => {
console.error(`Mongoose connection error: ${err}`);
process.exit(1);
});
require('./user');
require('./contact');
};
The index.js, user.js and contact.js files are all inside of my models folder. I can get this to run with no errors in console, but I'm still not seeing the contact collection on the mongo side. Am I on the right track here? As always any help or related posts are greatly appreciated. To get a better idea, here is the github link to the full project: https://github.com/DanDeller/Base. Thanks again guys.
via Daniel D
No comments:
Post a Comment