2016-08-24 45 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

相關問題