0
var mongoose = require('mongoose'),
Schema = mongoose.Schema;

var sensorModel = new Schema({
key: String,
value: Schema.Types.Mixed
})
modules.export = mongoose.model('collectionName',sensorModel);

メインの app.js ファイルからコレクションの名前を渡し、モデル コードで指定したいのですが、それを行う方法はありますか?

4

1 に答える 1

1

Template string以下のように試してみてください

センサーモデル.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');
于 2016-02-21T15:36:39.920 に答える