TypeError: db.collection is not a function

后端 未结 18 2462
青春惊慌失措
青春惊慌失措 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: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); 
        });
    
    });
    

提交回复
热议问题