首页 文章

无法读取未定义MongoDB的属性'collection'

提问于
浏览
1

我试图使用Node和MongoDB执行CRUD操作 .

我得到以下错误 .

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

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('/')
  })
})

Error trace:

TypeError:无法在Layer.handle上读取未定义的属性'collection'(/var/www/html/Express/SimpleCrud/server.js:20:21)[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 )在Layer.dhand上[/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)在/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)在/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:1 6)

我使用Mongodb 3.1

3 回答

  • 1

    嘿那里,

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

    更新:根据 mongodb package v3.x,DB的声明是单独完成的,而不是URL的一部分 . 更多信息:https://www.npmjs.com/package/mongodb

  • 0

    你有 var db 的多个声明,因此你退出范围后用于连接到mongo的数据库已经死了

    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

  • 2

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

相关问题