vadimdemedes / mongorito

🍹 MongoDB ODM for Node.js apps based on Redux
1.38k stars 90 forks source link

when i reconnect to mongodb, yield find not work #101

Open dnxbf321 opened 8 years ago

dnxbf321 commented 8 years ago
'use strict';

const mongorito = require('../');
const run = require('./run');

const Model = mongorito.Model;

class Post extends Model {

}

run(function * () {
    let posts;

    yield mongorito.connect('localhost/examples');

    // create
    let post = new Post({
        title: 'Great title'
    });

    yield post.save();

    posts = yield Post.find();
    console.log('0', posts); // here log success

    yield mongorito.disconnect();

    yield mongorito.connect('localhost/examples');

    posts = yield Post.find();
    console.log('1', posts); // not log success

    yield mongorito.disconnect();

    yield mongorito.connect('localhost/examples');

    posts = yield Post.find();
    console.log('2', posts); // not log success

    yield mongorito.disconnect();

});
vadimdemedes commented 8 years ago

Thanks for reporting. Any idea why it happens?

akanieski commented 8 years ago

I think I'm running into the same problem. I'm not sure yet what it could be. I'll post back if I notice.

hcjrabbit commented 7 years ago

I know that

let connection = MongoClient.connect(urls.join(','), options).then(function (db) {
        if (!self.db) {
            self.db = db;
        }

        return db;
    });

when call disconnect,but self.db is exsits, but old db is destory! If you want fix this question ,you should change this design , when you fix it . notify thx

kyungmi commented 7 years ago

I faced with this problem too. I agree with @hcjrabbit and I guess the same thing is happened with this._connection.

       let connection = MongoClient.connect(urls.join(','), options).then(function (db) {
        if (!self.db) {
            db.url = urls.join(',');
            self.db = db;
        }

        return db;
    });

    if (!this._connection) {
        this._connection = connection;
    }
vadimdemedes commented 7 years ago

V3 is out soon, hope it will fix the issue.