简体   繁体   中英

MongoClient not able to find collection

I'm working on a simple Node app (Express) and using MongoClient to establish the connection with my mongodb. The problem I'm running into is this: the application doesn't recognize the collection I'm using. However, the mongo shell retrieves both the db and the collection without a problem.

/app.js

var express = require('express');
var app = express();
var mongodb = require('mongodb')
var MongoClient = mongodb.MongoClient;
var ObjectId = mongodb.ObjectID;

app.set('view engine', 'ejs');
app.use(express.static('public'));

//connect to the 'b' database
var url = 'mongodb://localhost:27017/b';

MongoClient.connect(url, function(err, db) {
  if(err) return
  console.log("db server is running");

  //assign variable to the collection found in the database
  var Sb = db.collection('sb');

  app.get('/home', function(req, res) {
    db.Sb.find({ _id: ObjectId("56b20e547cea7eed08ca2a6c")}, function(err, data) {
      if (err) return
      res.render('show', {sb: data});
    });
  });
  db.close();
});

app.listen(3000, function() {
  console.log("client server is running");
});

Then, here is the output in the console:

[nodemon] starting `node app.js`
client server is running
db server is running
TypeError: Cannot read property 'find' of undefined
    at /Users/rachelroutson/workspace/stylyze_product_viewer/app.js:22:10
    at Layer.handle [as handle_request] (/Users/rachelroutson/workspace/stylyze_product_viewer/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/rachelroutson/workspace/stylyze_product_viewer/node_modules/express/lib/router/route.js:131:13)
    at Route.dispatch (/Users/rachelroutson/workspace/stylyze_product_viewer/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/Users/rachelroutson/workspace/stylyze_product_viewer/node_modules/express/lib/router/layer.js:95:5)
    at /Users/rachelroutson/workspace/stylyze_product_viewer/node_modules/express/lib/router/index.js:277:22
    at Function.process_params (/Users/rachelroutson/workspace/stylyze_product_viewer/node_modules/express/lib/router/index.js:330:12)
    at next (/Users/rachelroutson/workspace/stylyze_product_viewer/node_modules/express/lib/router/index.js:271:10)
    at SendStream.error (/Users/rachelroutson/workspace/stylyze_product_viewer/node_modules/serve-static/index.js:120:7)
    at emitOne (events.js:77:13)

Thank you!

You retrieved the collection with

var Sb = db.collection('sb);

You should then reference that collection directly using that variable. Instead of using db.Sb try

Sb.find({ _id: ObjectId("56b20e547cea7eed08ca2a6c")}, function(err, data) {
  if (err) return
  res.render('show', {sb: data});
});

The variable db does not have reference to an object Sb . That is a local variable that you have defined.

var url = 'mongodb://localhost:27017/b';
mongoClient.connect(url, function(err, db) {
  if(err) return
  console.log("db server is running");
  var Sb = db.collection('sb');

//home routing (adding a better route later)
  app.get('/home', function(req, res) {
    Sb.find({ _id:ObjectId("56b20e547cea7eed08ca2a6c")}).toArray(function(err, docs) {
      if(err) return
      var data = docs[0];
      res.render('show', {sb: data});
    });
  });
});

You need to use toArray() on the result of Sb.find() before you can render the object to the view. Then in the view file- call the attributes on the object like this:

<h2><%= sb._id %></h2>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM