ykn121
ykn121

Reputation: 855

TypeError: Cannot read property 'findOne' of undefined

Here are the codes

var mongo = require('mongodb');

var databaseName = 'Starter',
    collectionName = 'wines';

var Server = mongo.Server,
    Db = mongo.Db,
    BSON = mongo.BSONPure;

db = new Db(databaseName, server);

db.open(function(err, db) {
  if(!err) {
    console.log("Connected to 'winedb' database");
    db.collection(collectionName, {strict:true}, function(err, collection) {
        if (err) {
            console.log("The 'wines' collection doesn't exist. Creating it with sample data...");
            populateDB();
        }
    });
  }
});

exports.findById = function(req, res) {
  var id = req.params.id;
  console.log('Retrieving wine: ' + id);

  db.collection(collectionName, function(err, collection) {
      collection.findOne({'_id':new BSON.ObjectID(id)}, function(err, item) {
          res.send(item);
      });
  });

};

These codes are based on a sample of restful api for nodejs with mongoDB. However, it seems that it cannot recognise the function findOne. Can someone point out the problem?

Error message:

TypeError: Cannot read property 'findOne' of undefined

Upvotes: 5

Views: 32260

Answers (6)

satish chennupati
satish chennupati

Reputation: 2650

You represent your collection in a variable called collectionName and you are trying to invoke findOne() on something called collection which wont work.

instead of :

collection.findOne({'_id':new BSON.ObjectID(id)}, function(err, item)

use:

collectionName.findOne({'_id':new BSON.ObjectID(id)}, function(err, item)

Upvotes: 0

Gal Ratzkin
Gal Ratzkin

Reputation: 161

try to log collection to the console before using findOne or wrapping the whole thing in an:


if(err){log} else{findone}


      if(err){
      console.log(err)
      }
      else{
      collection.findOne({'_id':new BSON.ObjectID(id)}, function(err, item){
      res.send(item);
      }}

the reason it is not working is because collection in your error-first callback function is the parameter that gets assigned if the operation is successful ...

Useful link about the topic ....

Upvotes: 0

Priyanka Acharya
Priyanka Acharya

Reputation: 141

use this

collection.findOne({'_id':new mongo.ObjectID(id)}, function(err, item) {

instead of

collection.findOne({'_id':new BSON.ObjectID(id)}, function(err, item) {

in your wine.js file

Upvotes: 0

Sean Bradley
Sean Bradley

Reputation: 3577

change the start of your code to

var mongo = require('mongodb');
mongo.BSONPure = require('bson').BSONPure;

var databaseName = 'Starter',
    collectionName = 'wines';

var Server = mongo.Server,
    Db = mongo.Db,
    BSON = mongo.BSONPure;

Upvotes: 0

abdulbari
abdulbari

Reputation: 6232

findOne is deprecated in the latest version of [email protected]

https://github.com/mongodb/node-mongodb-native/blob/2.0/lib/collection.js

You can use this query instead

find(query).limit(1).next(function(err, doc){
   // handle data
})

Upvotes: 3

Fábio Santos
Fábio Santos

Reputation: 553

In the first db.collection you send 3 arguments like:

db.collection(collectionName, {strict:true}, function(err, collection)

After you only send 2:

db.collection(collectionName, function(err, collection)

This should be your issue.

Upvotes: 0

Related Questions