Javascript 无法将参数传递给mongo find集合
Req.params在执行db.collection.find后获取值。有人能告诉我这个代码有什么问题吗Javascript 无法将参数传递给mongo find集合,javascript,node.js,mongodb,node-mongodb-native,Javascript,Node.js,Mongodb,Node Mongodb Native,Req.params在执行db.collection.find后获取值。有人能告诉我这个代码有什么问题吗 exports.findAll = function(req, res) { var postal = parseInt(req.params.postal); db.collection('ifscdata', function(err, collection) { collection.find({'ADDRESS':/postal/}).toArray(
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);
示例URL:
为什么在
邮政
之间加斜杠
你试过这个吗
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);
});
});
看起来您需要在查询中使用正则表达式,考虑用如下对象包装变量:
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);
});
});
这就像mongodb中的部分比较。它检查字段中是否至少有一部分值。谢谢:)这很快:P