Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/417.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 无法将参数传递给mongo find集合_Javascript_Node.js_Mongodb_Node Mongodb Native - Fatal编程技术网

Javascript 无法将参数传递给mongo 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(

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(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