Question

Node.js, mongojs, mongodb. I am searching through a list of skills using regular expressions. Here is my server code:

var nconf = require('nconf');
var db = require('mongojs').connect(nconf.get('mongolab:connection'), ['skills', 'users']);

app.get('/api/skill', function(req, res){
    console.log('searching for ' + req.query['q']);

    var query = '{name: /' + req.query['q'] + '/i}';
    console.log('query: ' + query);
    db.skills.find(query, function(err, data){
        console.log('returning ' + JSON.stringify(data));
        if(!err){
            res.writeHead(200, {'content-type': 'text/json' });
            res.write( JSON.stringify(data) );
            res.end('\n');
        }   
    });

});

I do have a value of "asp.net" in my list. Console log outputs this:

searching for .net
query: {name: /.net/i} 
returning []

I use MongoHub to connect to the same server/db, paste the statement in query field, and get back my record:

{
    "name": "asp.net",
    "description": "",
    "_id": {
        "$oid": "500b4aae14f7960e91000001"
    }
}

Any suggestions?

Was it helpful?

Solution

query has to be an Object, not a string. Try this instead:

var query = {name: new RegExp(req.query['q'], 'i') };
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top