TypeError: db.collection is not a function

后端 未结 18 2278
青春惊慌失措
青春惊慌失措 2020-11-27 03:09

I am trying to post data to database that I have created on mLab and I am getting this error but I don\'t know whats going wrong.I also have read previously asked question o

相关标签:
18条回答
  • 2020-11-27 03:13

    Had this issue as well, I was following a tutorial in which the presenter was using the collection as a function. It never worked for me. What I discovered was that the presenter was using version 2.3.4 of the mongodb npm module. the module is well into version 3.x.x now. When I changed the package.json file to request the 2.x.x version of the mogodb npm module, suddenly everything worked.

    What I believed happened was that the module was altered to change the collection into a different object. Don't know how to use the new version but if you specify that you want the 2.x.x version, the old way should work. Specifically I can confirm that (coming from my package.json file, "dependencies" section) "mongodb": "^2.2.31" works.

    Best way:

    $> npm install mongodb@2.2.31 --save
    
    0 讨论(0)
  • 2020-11-27 03:13
    const MongoClient = require('mongodb').MongoClient;
    
    //connection url
    
     const url = 'mongodb://localhost:27017/myproject';
    
     MongoClient.connect(url,{useNewUrlParser: true},(err,client)=> {
      if(err) {
        return console.dir(err)
      }
    
       console.log('Connected to MongoDB')
    
      //get the collection
      let db = client.db('myproject');
      db.collection('users').insertOne({
      name: 'Hello World',
      email: 'helloworld@test.com'
    
      },(err,result)=> {
      if(err) {
          return console.dir(err)
      }
      console.log("Inserted Document");
      console.log(result);
    
         });
       });
    
    0 讨论(0)
  • 2020-11-27 03:14
    module.exports = function(app, db) {
      app.post('/notes', (req, res) => {
      const note = { text: req.body.body, title: req.body.title };
      db.collection('notes').insert(note, (err, result) => {
    ...
    

    db -> client

    module.exports = function(app, client) {
      var db = client.db("name");
      app.post('/notes', (req, res) => {
      const note = { text: req.body.body, title: req.body.title };
      db.collection('notes').insert(note, (err, result) => {
    ...
    
    0 讨论(0)
  • 2020-11-27 03:14

    In your package.json.

    make sure the following versions look like this:

    "nodemon": "^1.12.1"
    "mongodb": "^2.2.33"
    

    the above nodemon and mongodb versions work together without any errors. so your package.json should look something like this:

        {
      "name": "myapi",
      "version": "1.0.0",
      "description": "Json Api",
      "main": "server.js",
      "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1",
        "dev": "nodemon server.js"
      },
      "author": "Riley Manda",
      "license": "ISC",
      "dependencies": {
        "body-parser": "^1.18.2",
        "express": "^4.16.2",
        "mongodb": "^2.2.33"
      },
      "devDependencies": {
        "nodemon": "^1.12.1"
      }
    }
    

    dont forget to run npm install after downgrading

    0 讨论(0)
  • 2020-11-27 03:16

    Uninstalling existing mongodb package and reinstalling using the following commands resolved the issues for me. :)

    npm uninstall mongodb --save
    
    npm install mongodb@2.2.33 --save
    

    PS: Thanks to @MihirBhende and @yaxartes

    FYI,

    Prefer non-rc releases from https://github.com/mongodb/node-mongodb-native/releases, if you are new to the field.

    0 讨论(0)
  • 2020-11-27 03:17

    In your server.js, you are passing empty object where you need to pass database as second argument as its what your routes/index.js export function expects.

    PFB updated server.js :

    const express = require('express');
    const MongoClient = require('mongodb').MongoClient;
    const bodyParser = require('body-parser');
    
    const db = require('./config/db');
    
    const app = express();
    
    const port = 8000;
    
    app.use(bodyParser.urlencoded({extended:true}));
    
    MongoClient.connect(db.url,(err,database) =>{
    
        if (err) return console.log(err)
        //require('./app/routes')(app,{});
        //check below line changed
         require('./app/routes')(app, database);
        app.listen(port,() => {
            console.log("We are live on"+port); 
        });
    
    });
    
    0 讨论(0)
提交回复
热议问题