2016-11-03 19 views
0

コールバックと同期の問題を最小限に抑えるためにbluebirdを初めて使用しようとしています。次のように私はちょうど青い鳥でネイティブのMongoDBクライアントを使用しています:nodejs mongodb bluebirdエラー:db.getCollectionAsyncが関数ではありません

var mongodb = require('mongodb'); 
var Promise = require("bluebird"); 
Promise.promisifyAll(mongodb); 
var MongoClient = mongodb.MongoClient; 

を次に後でmodule.exportsはオブジェクトで、私が持っている:

foofunc: function(callback) { 
    MongoClient.connectAsync(url) 
    .then(function(db) { 
     //MongoDB should create collection if its not already there 
     console.log('... connect worked. OK now get bar collection'); 
     return db.getCollectionAsync('bar'); 
    }) 
    .then(function(col){ 
     //do something with the returned collection col 
    }) 
    .catch(function(err){ 
     //handle errors 
     console.log(err); 
     return callback(err); 
    }); 
    } 

は、私は私のサーバーが稼働してからこれを呼び出しますlocalhost。接続は動作しますが、その後、右の後、私はエラーを取得する: 未処理の拒否の例外TypeError:db.getCollectionAsyncが機能私が間違っているのは何

ではないでしょうか?私はサーバサイドですべてをやっているからですか?もしそうなら、どのようにAsyncの後ろに接尾辞が付いている接続が機能しますか?私の知る限り、あなたがネイティブのMongoDB NodeJsドライバを使用して見るように:-(

答えて

0

http://mongodb.github.io/node-mongodb-native/2.2/api/Db.html

このような場合は、その後、あなたが

return db.collection('bar');

を使用する必要があります。またポイントこのメソッドが同期的であることをここにノードに渡します。

この回答は役に立ちます。

How can I promisify the MongoDB native Javascript driver using bluebird?

希望します。

+0

OMG !!!ドキュメントのページも似ています。私はhttp://mongodb.github.io/node-mongodb-native/2.2/api/Collectionの代わりにhttps://docs.mongodb.com/v2.6/reference/method/db.getCollection/を見つめていました。 html !!!あまりにも多くのすべてが今働くおかげで:) – rstruck

関連する問題