0

我在我的代码和 mongodb 中使用 twitter API。反映了数据库中的正确输出,但并未终止。我想问题出db.server.find({id:myid},cb);在下面代码中的语句上。但是,我不知道如何解决。

var Twit = require('../lib/twitter'),
    conf = require('../config1');
var myid;
var twit = new Twit(conf);
var databaseUrl = "mydb2"; // "username:password@example.com/mydb"
var collections = ["server", "followers"];
var db = require("mongojs").connect(databaseUrl, collections);
twit.get('account/verify_credentials', function (err, reply) {
    myid = reply.id;

    function addToServer(myid, cb) {
        db.server.find({
            id: myid
        }, cb);
    };

    addToServer(myid, function (err, resp) {
        if (err) {
            console.log("err");
        } else if (resp.length > 0) {
            console.log("My Id present in server present");
        } else {
            console.log("New to the app.So updating server  ");
            db.server.insert({
                id: myid
            });
            db.followers.insert({
                id: myid,
                following: []
            })
        }
    });
});

PS:这是我代码的一部分,我也使用过process.exit(0)函数,但仍然没有帮助。

4

1 回答 1

0

I think your issue is related to this: https://github.com/mafintosh/mongojs/issues/15.

Here's a gist. If I call db.close() the program exists, and if I don't, it doesn't. So process.on('exit') must not be the right place to call it.

But the issue is that that you have a persistent tcp connection open to the DB, and as long as that's running, the script won't shut down.

Is this a run-once script, or do you need to keep this thing running?

EDIT:

Since the script only needs to run once, I'd use callbacks on your 2 database queries and close the database down in the last callback.

于 2014-01-14T21:48:00.137 回答