Sunday, 4 June 2017

Find() Method MongoDB with Nodejs

I'm currently trying to find all the documents that have a certain value of 'bar' in the key 'foo'. Using mongodb and nodejs.

When I try to run this I get: "TypeError: Cannot read property 'find' of undefined" error return.

If I try using findOne() it'll just return the first document that has the document with the value "bar" for the key "foo", however there are 3.

module.exports = function(app, db) {

app.get('/foo', (req, res)=>{

db.collection('barCollection').find({foo: {$all: ['bar']}}
,(err, item)=>{
 if (err) {
            res.send({'error':'An error has occurred'});
        } else {
            res.send(item);
        } 
    });
 });
};

Thanks



via rudster

No comments:

Post a Comment