When i am trying to run this code i am getting the following error

12,799
mongoose.connect('mongod://localhost/students', { useNewUrlParser: true }); 

It should start with mongodb://..., And When you use { useNewUrlParser: true } you should always specify the port number in connection string mongoose reference like given below.

mongoose.connect('mongodb://localhost:27017/students', { useNewUrlParser: true });

here port number 27017 is the default port for MongoDB database.

Share:
12,799
Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin almost 2 years

    I am trying to connect mongodb database by this code but when running this code I get the error(error at the bottom of the code). One of the error was in the 7th line where it was resolved by adding { useNewUrlParser: true } but still it has more errors. I am using MongoDB version 4.0.1. Do anybody know how to resolve this error.

    const express = require('express');     
    const mongoose = require('mongoose');  
    const bodyParser = require('body-parser');   
    const app = express();
    
    app.use(express.static('public'));  
    app.set('view engine','ejs');
    
    mongoose.connect('mongod://localhost/students', { useNewUrlParser: true });  
    var studentSchema = new mongoose.Schema({  
        name: String,  
        age:Number,  
        country:String  
    });
    
    var Student = mongoose.model("Student", studentSchema);  
    var shashank = new Student({  
        name:"Shashank",  
        age:"21",  
        country:"India"  
    });    
    
    shashank.save((err,student) => {    
        if(err){     
            console.log('Something went wrong!');     
        }    
        else {      
            console.log("You added: " + student);      
        }      
    });      
    
    app.listen(3000,() => {     
        console.log('Server Listening!');     
    });
    

    Error while running this code!

    D:\HARDWORK\YelpCamp-Course>node app.js
    Server Listening!
                                                                                                   (node:14060) UnhandledPromiseRejectionWarning: MongoParseError: Invalid connection string
        at parseConnectionString (D:\HARDWORK\YelpCamp-Course\node_modules\mongodb-core\lib\uri_parser.js:216:21)
        at connect (D:\HARDWORK\YelpCamp-Course\node_modules\mongodb\lib\operations\mongo_client_ops.js:179:3)
        at connectOp (D:\HARDWORK\YelpCamp-Course\node_modules\mongodb\lib\operations\mongo_client_ops.js:283:3)
        at executeOperation (D:\HARDWORK\YelpCamp-Course\node_modules\mongodb\lib\utils.js:420:24)
        at MongoClient.connect (D:\HARDWORK\YelpCamp-Course\node_modules\mongodb\lib\mongo_client.js:168:10)
        at Promise (D:\HARDWORK\YelpCamp-Course\node_modules\mongoose\lib\connection.js:499:12)
        at new Promise (<anonymous>)
        at NativeConnection.Connection.openUri (D:\HARDWORK\YelpCamp-Course\node_modules\mongoose\lib\connection.js:496:19)
        at Mongoose.connect (D:\HARDWORK\YelpCamp-Course\node_modules\mongoose\lib\index.js:229:15)
        at Object.<anonymous> (D:\HARDWORK\YelpCamp-Course\app.js:9:10)
        at Module._compile (internal/modules/cjs/loader.js:689:30)
        at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
        at Module.load (internal/modules/cjs/loader.js:599:32)
        at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
        at Function.Module._load (internal/modules/cjs/loader.js:530:3)
        at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
    (node:14060) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 3)                                                                                                                (node:14060) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
    
  • Admin
    Admin over 5 years
    Thanks! Many errors are gone, but there are few more, please have a look. (node:6516) UnhandledPromiseRejectionWarning: #<Object> (node:6516) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1) (node:6516) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
  • Pavan Vora
    Pavan Vora over 5 years
    wait lemme take a look.