Node.js 为什么我的nodejs脚本没有';你不辞职去壳牌吗?

Node.js 为什么我的nodejs脚本没有';你不辞职去壳牌吗?,node.js,mongojs,Node.js,Mongojs,我的代码如下所示: #!/bin/env node var collection = require('mongojs')('test').collection('test'); collection.findOne({}, function(err, doc) { console.log(err, doc); }); 当我运行此脚本时,它显示: $ node test.js null null 但是剧本没有退出。我需要“CTRL+C”来退出它。有人知道怎么修理吗 [更新] 我发

我的代码如下所示:

#!/bin/env node

var collection = require('mongojs')('test').collection('test');

collection.findOne({}, function(err, doc) {
    console.log(err, doc);
});
当我运行此脚本时,它显示:

$ node test.js
null null
但是剧本没有退出。我需要“CTRL+C”来退出它。有人知道怎么修理吗

[更新]

我发现,如果我使用原生mongodb而不是mongojs,就没有问题了:

#!/bin/env node

var client = require('mongodb');

client.connect('mongodb://127.0.0.1:27017/hatch', function(err, db) {
    if (err) throw err;
    var collection = db.collection('documents');

    collection.find().toArray(function(err, results) {
        console.dir(results);
        db.close();
    });
});

这是mongojs的问题吗?

您必须关闭数据库连接

var mongojs = require('mongojs');
var db = mongojs('test');
var collection = db.collection('test');

collection.findOne({}, function(err, doc) {
    console.log(err, doc);
    db.close();
});

您必须关闭数据库连接

var mongojs = require('mongojs');
var db = mongojs('test');
var collection = db.collection('test');

collection.findOne({}, function(err, doc) {
    console.log(err, doc);
    db.close();
});

看到我的答案,你必须关闭连接。看到我的答案,你必须关闭连接。