Uncaught TypeError: Cannot read property ‘collection’ of null

Tags: , , , ,

I’ve been trying to get data out of a collection, but it returns me Uncaught TypeError: Cannot read property ‘collection’ of null. The Mongo database itself is connected with the cloud and checking from there the collection with that name exists.

    var output = [];

mongoose.connect(MongoURI, { useNewUrlParser: true, useUnifiedTopology: true }, function(client) {
    var cursor = client.collection('updates').find();
    cursor.forEach(function(values) {
        output += values;

I have planned to later use the output for condition check to see, if there are any similar entries.


As official docs states, mongoose.connect accepts callback for error handling as last argument. https://mongoosejs.com/docs/4.x/docs/connections.html

So to find data, you should pass model name and it’s schema to mongoose.model, retreive collection, and then seek for what you need. For instance:

const client = mongoose.model("Client", clientScheme);

client.find({}, function(err, docs){
    if(err) return console.log(err);

Take a glance at https://mongoosejs.com/docs/guide.html

Source: stackoverflow