无法阅读财产'收集'未定义的MongoDB

时间:2018-01-02 09:38:49

标签: node.js mongodb

我尝试使用Node和MongoDB执行CRUD操作。

我得到以下错误。

  

TypeError:无法读取属性'集合'未定义的

const express = require('express')
const bodyParser= require('body-parser')
const MongoClient = require('mongodb').MongoClient
const app = express()
app.use(bodyParser.urlencoded({extended: true}))

const url = 'mongodb://localhost:27017/testdb';

var db
MongoClient.connect(url, function(err, client) {
  if (err) return console.log(err)
  var db = client.db('testdb');

})

app.post('/quotes', (req, res) => {
    var collectionName = 'quotes';
    var collection = db.collection(collectionName);
    collection.save(req.body, (err, result) => {
    if (err) return console.log(err)

    console.log('saved to database')
    res.redirect('/')
  })
})

错误跟踪:

  

TypeError:无法读取属性'集合'未定义的       在app.post(/var/www/html/Express/SimpleCrud/server.js:20:21)       在Layer.handle [as handle_request](/var/www/html/Express/SimpleCrud/node_modules/express/lib/router/layer.js:95:5)       在下一个(/var/www/html/Express/SimpleCrud/node_modules/express/lib/router/route.js:137:13)       在Route.dispatch(/var/www/html/Express/SimpleCrud/node_modules/express/lib/router/route.js:112:3)       在Layer.handle [as handle_request](/var/www/html/Express/SimpleCrud/node_modules/express/lib/router/layer.js:95:5)       at /var/www/html/Express/SimpleCrud/node_modules/express/lib/router/index.js:281:22       在Function.process_params(/var/www/html/Express/SimpleCrud/node_modules/express/lib/router/index.js:335:12)       在下一个(/var/www/html/Express/SimpleCrud/node_modules/express/lib/router/index.js:275:10)       at /var/www/html/Express/SimpleCrud/node_modules/body-parser/lib/read.js:130:5       在invokeCallback(/var/www/html/Express/SimpleCrud/node_modules/raw-body/index.js:224:16)

我正在使用Mongodb 3.1

2 个答案:

答案 0 :(得分:2)

这是一个简单的关闭问题 var是本地/全局范围,您有var db

的多个声明
var db  <====== first declaration which is undefined 
MongoClient.connect(url, function(err, client) {
 if (err) return console.log(err)
 var db = client.db('testdb');   <======= second declaration where u connected to 
 the db which is undefined out of this scope

})

我个人更喜欢猫鼬,只要它不是打字稿:D

几个例子:

const MongoClient = require('mongodb').MongoClient,
Server = require('mongodb').Server;

const mongoClient = new MongoClient(new Server('localhost', 27017));
mongoClient.open(function(err, mongoClient) {
const db1 = mongoClient.db("mydb");

mongoClient.close();
});

const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');

// Connection URL
const url = 'mongodb://localhost:27017';

// Database Name
const dbName = 'myproject';

//Use connect method to connect to the server
MongoClient.connect(url, function(err, client) {
  assert.equal(null, err);
  console.log("Connected successfully to server");

  const db = client.db(dbName);

  client.close();
});

source

答案 1 :(得分:0)

代码中存在简单的错误。在与mongodb服务器建立连接之前,您已启动了http服务器。这就是当你的post api加载到内存中时变量db未定义的原因。由于与mongodb的连接是异步的。为了利用mongodb实例,您必须首先连接mongdb服务器,然后启动http服务器并将数据库实例全局存储或存储在您需要的某个模块中,并获取数据库实例以执行查询。