Home>

Currently, we are creating a clone application for the Amazon site using Node.js and Vue.js.
When implementing server logic with Node.js and starting the server, the following error occurred.

Error message
[nodemon] 2.0.1
[nodemon] to restart at any time, enter `rs`
[nodemon] watching dir (s): *. *
[nodemon] watching extensions: js, mjs, json
[nodemon] starting `node server.js`
Listening on PORT 3000
{MongoParseError: Invalid connection string
    at parseConnectionString (/Users/user/amazon/server/node_modules/mongodb/lib/core/uri_parser.js:507:21)
    at connect (/Users/user/amazon/server/node_modules/mongodb/lib/operations/connect.js:268:3)
    at ConnectOperation.execute (/Users/user/amazon/server/node_modules/mongodb/lib/operations/connect.js:191:5)
    at executeOperation (/Users/user/amazon/server/node_modules/mongodb/lib/operations/execute_operation.js:83:26)
    at MongoClient.connect (/Users/user/amazon/server/node_modules/mongodb/lib/mongo_client.js:216:10)
    at Promise (/Users/user/amazon/server/node_modules/mongoose/lib/connection.js:638:12)
    at new Promise ()
    at NativeConnection.Connection.openUri (/Users/user/amazon/server/node_modules/mongoose/lib/connection.js:635:19)
    at Mongoose.connect (/Users/user/amazon/server/node_modules/mongoose/lib/index.js:328:15)
    at Object.  (/Users/user/amazon/server/server.js:11:10)
    at Module._compile (internal/modules/cjs/loader.js: 778: 30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js: 789: 10)
    at Module.load (internal/modules/cjs/loader.js: 653: 32)
    at tryModuleLoad (internal/modules/cjs/loader.js: 593: 12)
    at Function.Module._load (internal/modules/cjs/loader.js: 585: 3)
    at Function.Module.runMain (internal/modules/cjs/loader.js: 831: 12)
  name: 'MongoParseError',
  [Symbol (mongoErrorContextSymbol)]: {}}
Applicable source code
const express = require ('express')
const morgan = require ('morgan')
const bodyParser = require ('body-parser')
const mongoose = require ('mongoose')
const dotenv = require ('dotenv')
dotenv.config ()
const app = express ();
mongoose.connect (
  process.env.DATABASE,
  {useNewUrlParser: true, useUnifiedTopology: true},
  err =>{
    if (err) {
      console.log (err);
    } else {
      console.log ('Connected to the database');
    }
  }
)
// Middlewares
app.use (morgan ('dev'))
app.use (bodyParser.json ())
app.use (bodyParser.urlencoded ({extended: false}))
// GET-Retrieve data from the server
app.get ('/', (req, res) =>{
  res.json ('Hello amazon clone')
})
// POST-send data from fronted to backend
app.post ('/', (req, res) =>{
  console.log (req.body.name);
})
app.listen (3000, err =>{
  if (err) {
    console.log (err);
  } else {
    console.log ('Listening on PORT', 3000);
  }
})

Click here for reference, npm i dotenv command input,
In server.js

const dotenv = require ('dotenv')
dotenv.config ()

Even if

was implemented and nodemon server.js was entered again, it did not solve the problem.

Supplemental information (package.json)

{
"name": "server.js",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \" Error: no test specified \ "&&exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^ 1.19.0",
"dotenv": "^ 8.2.0",
"express": "^ 4.17.1",
"mongodb": "^ 3.3.4",
"mongoose": "^ 5.7.12",
"morgan": "^ 1.9.1",
"npm": "^ 6.13.1"
}
}

  • Answer # 1

    In server.js

      

    mongoose.connect (
        process.env.DATABASE,

    Since it is

    , it seems that the DATABASE environment variable is not set correctly.

    If you use the

    dotenv package, create a file.envin the project directory and list the environment variables that the program needs.

    # Add the following description to .env (Rewrite MongoDB connection information as necessary)
    DATABASE = mongodb: // localhost/xxxxxx

    Alternatively, you may want to set the DATABASE environment variable before starting the server in the shell.
    (No dotenv required in this case)

    export DATABASE = "mongodb: // localhost/xxxxxx"
    nodemon server.js

Related articles