'Error in connecting Database(MongoDB) with express server
As soon as I am connecting my database with express using
const db = require("./config/mongoose");
It shows error
SyntaxError: Invalid or unexpected token
at wrapSafe (internal/modules/cjs/loader.js:979:16)
at Module._compile (internal/modules/cjs/loader.js:1027:27)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Module.require (internal/modules/cjs/loader.js:952:19)
at require (internal/modules/cjs/helpers.js:88:18)
at Object.<anonymous> (C:\Users\DELL\Documents\Vs Code Folders\Feelare\node_modules\whatwg-url\lib\url-state-machine.js:8:31)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
[nodemon] app crashed - waiting for file changes before starting...
How can I get out of it?
Solution 1:[1]
This type of error generally comes when you do not provide valid JSON data.
For ex:-
JSON.stringify({x:})
return error like this
Uncaught SyntaxError: Unexpected token '}'
Please check your provided JSON data again.
And after that you still get error please show your mongoose connection code.
Solution 2:[2]
This code is working fine. I have not got any error. I got successful connection message in console Connected to Database:: MongoDB.
Try this code it will also work for you.
const mongoose = require("mongoose");
mongoose.connect("mongodb://localhost/feelare_db");
const db = mongoose.connection;
db.on("error", console.error.bind(console, "Error in connecting to MongoDB "));
db.once("open", function () {
console.log("Connected to Database:: MongoDB");
});
module.exports = db;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Harsh Mangalam |
| Solution 2 | Harsh Mangalam |
