MongoClient无法找到集合

时间:2016-02-12 16:30:28

标签: node.js mongodb express

我正在使用一个简单的Node应用程序(Express)并使用MongoClient与我的mongodb建立连接。我遇到的问题是:应用程序无法识别我正在使用的集合。但是,mongo shell可以毫无问题地检索db和集合。

/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");
});

然后,这是控制台中的输出:

[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)

谢谢!

2 个答案:

答案 0 :(得分:0)

您使用

检索了该集合
var Sb = db.collection('sb);

然后,您应该使用该变量直接引用该集合。而不是使用db.Sb尝试

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

变量db没有引用对象Sb。这是您定义的局部变量。

答案 1 :(得分:0)

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});
    });
  });
});

在将对象渲染到视图之前,需要对Sb.find()的结果使用toArray()。然后在视图文件中调用对象上的属性,如下所示:

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