0

I'm implementing a tag system in my page. When user entry any string it will trigger my express route and try to find the content typed to fill a list and return it as JSON to process the callback. The problem is about moongose, because the query is pretty right and works fine if I execute it on mongo shell.

The query:

var token = '/.*' + req.query.search + '.*/i';
Tag.find({ description: token , inactivatedAt: null }, function(err, tags) {
    var tempArray = [];

    if (tags) {
        var counter = 0;

        tags.forEach(function(tag) {
            var dArray = [];

            dArray.push(counter++);
            dArray.push(tag.description);
            dArray.push(null);
            dArray.push(null);
            tempArray.push(dArray);
        });
    }

    res.writeHead(200, {'content-type': 'text/json'});
    res.end(JSON.stringify(tempArray));
});

Even if I query as description: { $regex: token } or description: token the result is not found. The same query parameters are fine and brings result from the mongo on the shell. E.g.: db.tags.find({ description: /.*MET.*/i , inactivatedAt: null }).pretty() but my mongoose query doesn't work.

Any clue will be very helpful. Thanks in advance!

4

1 回答 1

2

You need to convert token into a regular express instead of just a string:

var token = new RegExp(req.query.search, 'i');
于 2013-02-11T00:17:47.337 回答