2016-08-24 44 views
1

当我运行下面的代码,它顺序连接到MongoDB,然后关闭,在javascript中使用async.waterfall,该程序不会按预期结束。相反,它出现在“DB封闭的”线后,只是等待。为什么我的async.waterfall JavaScript时序代码没有结束?

$ node test-async2.js 
hit connectMongo 
Connected correctly to server, DB: notes 
hit closeMongo 
DB closed 

[program just waits here, doesn't end] 

我期待的节目结束了。我在做什么错误?

const 
    async = require('async'), 
    MongoClient = require('mongodb').MongoClient, 
    url = 'mongodb://localhost:27017/notes';  

function connectMongo(next) { 
    console.log('hit connectMongo'); 
    MongoClient.connect(url, function(err, db) { 
    console.log("Connected to server, DB: " + db.databaseName); 
    next(null, db); 
    }); 
} 

function closeMongo(db, next) { 
    console.log('hit closeMongo'); 
    db.close; 
    next(null, "DB closed"); 
} 

// perform connect then close sequentially 
async.waterfall([ 
    connectMongo, 
    closeMongo, 
], function (err, result) { 
    if (err) throw err; 
    console.log(result); 
}); 
+0

你说的'写过代码什么程序end.'?意思是,这是什么我想'ctrl + c'会有帮助你停止你的程序。我不认为它会自动结束。 – Shrabanee

+2

你打电话给'close'吗?没有括号。 – cartant

+1

@cartant - 就是这样,非常感谢。我一直盯着代码这么久,我只是看不到一个愚蠢的错误。再次感谢 !!! – Jon

回答

2

尝试db.close()代替db.close

此外,添加一个回调db.close检查错误而关闭。

+1

非常感谢@Tom,我看不到在我面前的愚蠢错误。关于检查关闭错误的好处 - 再次感谢。 – Jon

相关问题