node.js - switching database with mongoose -
hi there way switch database mongoose? thought that:
mongoose.disconnect(); mongoose.connect('localhost',db);
but not work receive error:
error: trying open unclosed connection.
i not know if because asynchronous
it asynchronous. if pass callback function disconnect , try connect next database in callback, work.
ex.
var mongoose = require('mongoose') mongoose.connect('mongodb://localhost/test1', function() { console.log('connected test 1') mongoose.disconnect(connecttotest2) }) function connecttotest2() { mongoose.connect('mongodb://localhost/test2', function() { console.log('connected test 2') process.exit() }) }
Comments
Post a Comment