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);
});
你说的'写过代码什么程序end.'?意思是,这是什么我想'ctrl + c'会有帮助你停止你的程序。我不认为它会自动结束。 – Shrabanee
你打电话给'close'吗?没有括号。 – cartant
@cartant - 就是这样,非常感谢。我一直盯着代码这么久,我只是看不到一个愚蠢的错误。再次感谢 !!! – Jon