Reputation: 83
I have 2 collections that I declared in my comments.model file:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
require('./util');
var currentDate = new Date().getDate();
var currentMonth = new Date().getMonth()+1;
var currentYear = new Date().getFullYear();
var battleFieldOneCommentsSchema = new Schema( {
user_name: {type: String},
comment: {type: String},
date_created: {type: String, default: String(currentDate+"/"+currentMonth+"/"+currentYear)},
likes: {type: Number, default: 0},
dislikes: {type: Number, default: 0}
});
module.exports = mongoose.model('battlefieldOne_Comments', battleFieldOneCommentsSchema);
module.exports = mongoose.model('another_game_Comments', battleFieldOneCommentsSchema);
I have an index.js file that has API's to insert comments into the database:
var battlefieldOne_Comments = require('../models/comments');
var anotherGame_Comments = require('../models/comments');
router.post('/add_battlefieldOne_Comment', function(req, res, next) {
comment = new battlefieldOne_Comments(req.body);
comment.save(function (err, savedComment) {
if (err)
throw err;
res.json({
"id": savedComment._id
});
});
});
router.post('/add_anotherGame_Comments', function(req, res, next) {
comment = new anotherGame_Comments(req.body);
comment.save(function (err, savedComment) {
if (err)
throw err;
res.json({
"id": savedComment._id
});
});
});
module.exports = router;
When I use that API, it inserts the same comment into both collections on the database. I know this is because both comments variables in the index.js file require the same file. Is there a way to fix this, because I don't want to make a new model file for each schema. I am new to nodejs and mongoose, so this might be a silly question, but is there a way to define a single schema and use that schema for a lot of collections, while still being able to update those collections separately and independently?
Upvotes: 2
Views: 1951
Reputation: 1542
The way you are exporting and requiring your models in index.js
won't have the effect you want.
When you use module.exports
like that you're not giving the values you're exporting a name, so when require
is called on that file, you'll end up requiring the same value for both of your variables.
What you wanna do here is set your models to different variables and then export those:
var battlefieldOneComments = mongoose.model('battlefieldOne_Comments', battleFieldOneCommentsSchema);
var anotherGameComments = mongoose.model('another_game_Comments', battleFieldOneCommentsSchema);
module.exports = {
battlefieldOneComments : battlefieldOneComments,
anotherGameComments : anotherGameComments
}
After that you require them by accessing those in index.js
:
var battlefieldOne_Comments = require('../models/comments').battlefieldOneComments;
var anotherGame_Comments = require('../models/comments').anotherGameComments;
That way you're not requiring the same model for both variables and it should save your comments on different collections.
Upvotes: 3