2017-03-20 6 views
0

私はAPIを構築しようとしていますが、私はロードブロッキングを見つけました。 私の "スタイル"コレクションには一意でない "Makes"がたくさんあります。ユーザー入力(例えば:Sedan)からカテゴリを持つすべての "Makes"を取得し、カテゴリ比較に合格するユニークな "Make"オブジェクトのみを表示します。MongoDB DistinctとExpressを使用して検索

注:私は 'Mongoose'を使用していません。私はネイティブドライバを使用しています。

distinctfindの方法(Expressを使用して)をチェーンするにはどうすればよいですか?次のコードで

router.get('/makes/:category', function(req, res) { 
    var collection = db.get().collection('styles') 

    console.log(req.params.category) 

    collection.distinct('make').find({'categories.vehicleStyle': req.params.category}, {'make': 1}).toArray(function(err, docs) { 
    res.send(docs) 
    }) 
}) 

予想される出力:

TypeError: collection.distinct(...).find is not a function 
    at C:\path\server\routes\api.js:67:31 
    at Layer.handle [as handle_request] (C:\path\node_modules\express\lib\router\layer.js:95:5) 
    at next (C:\path\node_modules\express\lib\router\route.js:137:13) 
    at Route.dispatch (C:\path\node_modules\express\lib\router\route.js:112:3) 
    at Layer.handle [as handle_request] (C:\path\node_modules\express\lib\router\layer.js:95:5) 
    at C:\path\node_modules\express\lib\router\index.js:281:22 
    at param (C:\path\node_modules\express\lib\router\index.js:354:14) 
    at param (C:\path\node_modules\express\lib\router\index.js:365:14) 
    at Function.process_params (C:\path\node_modules\express\lib\router\index.js:410:3) 
    at next (C:\path\node_modules\express\lib\router\index.js:275:10) 
+1

を提供していますが、 'collection.distinct( '作る'、{ 'categories.vehicleStyle' を試すことができます})。toArray(function(err、docs){ res.send(docs) }) ' – Veeram

+0

これは機能します!本当にありがとう!! – Moshe

答えて

関連する問題