2016-04-29 68 views
1

執行db.collection.find後,Req.params將獲取值。可以有人告訴我我在做什麼這個代碼錯了嗎?無法將參數傳遞給mongo查找集合

exports.findAll = function(req, res) { 
    var postal = parseInt(req.params.postal); 
    db.collection('ifscdata', function(err, collection) { 
     collection.find({'ADDRESS':/postal/}).toArray(function(err, items) { 
      res.send(items); 
     }); 
    }); 

上午應該根據郵件做地址部分搜索。但是因爲它只有在獲得價值之後才能將價值轉嫁給郵政。

函數路徑是這樣的

app.get('/ifsc/:postal', ifsc.findAll); 

樣品網址:

http://localhost:3000/ifsc/691009

回答

1

看起來你需要你的查詢中使用正則表達式,考慮與RegExp對象包裹變量,如下所示:

exports.findAll = function(req, res) { 
    var postal = req.params.postal, 
     regex = new RegExp(postal); 

    db.collection('ifscdata', function(err, collection) { 
     collection.find({'ADDRESS': regex}).toArray(function(err, items) { 
      res.send(items); 
     }); 
    }); 
+1

謝謝:)而且很快:P – user2038580

0

爲什麼你把postal之間的斜線?

你試過這個嗎?

exports.findAll = function(req, res) { 
    var postal = parseInt(req.params.postal); 
    db.collection('ifscdata', function(err, collection) { 
     collection.find({'ADDRESS': postal}).toArray(function(err, items) { 
      res.send(items); 
     }); 
    }); 
+0

它像MongoDB中的部分比較。它檢查價值的至少一部分是否在現場。 – user2038580