Filter with Node.JS

1

I have a list of saved items in a Mongo collection, I'd like to know how to filter the original array. Follow the code

api.filtra = function (req, res) {
    model.find()
        .then(function(itens) {
            for(i = 0; i < itens.length; i++)
                console.log(itens[i].criterio);                
                res.json(itens);

                console.log("testeA");
                var filtrado = itens.filter(
                    itens, {"genero": "0"}
                );
                console.log("testeB");               
                console.log(filtrado);
        }, function (error) {
            console.log(error);
            res.sendStatus(500);
        });
}

The problem with this code is that it crashes when it's time to run "itens.filter", the last thing it displays on the console is "testA" Home What I would like to do is, for example, say I have people in this array, pass as a parameter via url the age, more or less like '/ v1 / people / age / x' where x is the age chosen by the user, and so just send the list to the browser with people who already meet this criteria. Home I hope I have been able to explain my doubts well, but if you have been confused tell me that I will try to rephrase my question. Home Thank you all right away.

    
asked by anonymous 03.09.2017 / 07:42

1 answer

1

Why do not you let mongodb do this for you, search on mongodb using the querry of it ... ex:

model.find({idade:req.query.idade}).then((result) => {
     res.json(result)
}, (err) => {
     res.status(500).json({error: err})
})

This way you could call it like this: / api / people? age = 15. I recommend that you study one of the standards of an api RestFull

    
05.09.2017 / 15:12