Reputation: 425
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var sensorModel = new Schema({
key: String,
value: Schema.Types.Mixed
})
modules.export = mongoose.model('collectionName',sensorModel);
I want to pass the the name of collection from my main app.js file to it then specifying it in the model code, is there a way by which I can do that?
Upvotes: 0
Views: 689
Reputation: 48346
Please try it with Template string
as below
sensorModel.js
module.exports = function (modelName) {
// sensorModel definition...
//...
var str = `${modelName}`;
mongoose.model(str, sensorModel);
});
app.js
var modelName = 'collectionName';
require('./models/sensorModel.js')(modelName); // require the model file before invoking `mongoose.model`.
var CollectionName = mongoose.model('collectionName');
Upvotes: 1