无法将parameter passing给mongo查找集合
在执行db.collection.find之后,Req.params获取值。 可以有人告诉我,我做错了这个代码?
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); }); });
我们应该根据邮政地址进行部分search。 但是因为它只有在获得价值之后才能通过邮寄的价值。
function路线是这样的
app.get('/ifsc/:postal', ifsc.findAll);
示例url:
HTTP://本地主机:3000 / IFSC / 691009
看起来您需要在查询中使用正则expression式,请考虑使用RegExp
对象包装variables,如下所示:
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); }); });
你为什么把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); }); });