Reputation: 1546
In my connections.js I have specified one mongodb connection:
someMongodbServer: {
adapter: 'sails-mongo',
host: 'localhost',
port: 27017,
user: '',
password: '',
database: 'your_mongo_db_name_here'
}
and in my controller I have overwritten the database name like this:
module.exports = {
sync: function (req, res) {
var params = req.params.all();
sails.config.connections.someMongodbServer.database = "newDynamicName";
console.log(sails.config.connections.someMongodbServer);
User.create(params, function(err, users) {
if (err) return next(err);
res.status(201);
res.json(users);
});
}
};
but it still uses the name specified in connections.js (that database is created as soon as the application is lifted)
In case I don't provide any database name in connections.js, by default it will create and use a one named sails
I need to create a database in my controller with a dynamic name and insert new rows there after I create them. Any help here?
Upvotes: 2
Views: 729
Reputation: 11
The problem here is that the database name is static, meaning it never over written. As you have assigned 'your_mongo_db_name_here' to database variable. There for no matter what you assign to database name before this call, it will never be assigned to the database name.
There for the solution would to be have a constructor parameter or create as function
Upvotes: 1