Here is my error captured:
and here are my coding files:
server file
keys file
I just create connection to mlab using mongodb, node and reactjs. I'm using mongo db version 4.
need your help guys.
[nodemon] starting `node server.js`
Error: URL malformed, cannot be parsed
at module.exports (C:\MERNapps\TestMERN08\node_modules\mongodb\lib\url_parser.js:17:21)
at deprecated (internal/util.js:47:15)
at connect (C:\MERNapps\TestMERN08\node_modules\mongodb\lib\operations\mongo_client_ops.js:179:3)
at connectOp (C:\MERNapps\TestMERN08\node_modules\mongodb\lib\operations\mongo_client_ops.js:283:3)
at executeOperation (C:\MERNapps\TestMERN08\node_modules\mongodb\lib\utils.js:420:24)
at MongoClient.connect (C:\MERNapps\TestMERN08\node_modules\mongodb\lib\mongo_client.js:168:10)
at Promise (C:\MERNapps\TestMERN08\node_modules\mongoose\lib\connection.js:493:12)
at new Promise (<anonymous>)
at NativeConnection.Connection.openUri (C:\MERNapps\TestMERN08\node_modules\mongoose\lib\connection.js:490:19)
at Mongoose.connect (C:\MERNapps\TestMERN08\node_modules\mongoose\lib\index.js:230:15)
at Object.<anonymous> (C:\MERNapps\TestMERN08\server.js:12:2)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
(node:3696) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
server running on port 5000
The issue is with your keys. Try using:
module.exports= {
mongoURI : "your-uri-string",
options:{key:value}
}
And use the keys as:
mongoose.connect(db.mongoURI,options);
a very common error with MongoDB atlas. better still follow this procedure
Install Mongo DB compass
use Default setting
hostname: localhost & port 27017
create a database with the project name and then collection: user
Connect with the code below on entry js. route
mongoose.connect('mongodb://localhost/projectname')
.then(() => console.log('mongo db working'))
.catch(err => console.log('ERROR is', err))
Related
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?
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.
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;
I am learning rest api but keep on getting the following error. Why? I am using Node JS, Mongo DB and Express. I am new to this.
Code:
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
//connect to mongoose
mongoose.connect('mongodb://localost/bookstore');
var db = mongoose.connection;
app.get('/', function(req, res){
res.send('Please use /api for the API.');
});
app.listen(3000);
console.log('Running on port 3000...');
Error:
(node:7908) DeprecationWarning: current URL string parser is deprecated, and wil
l be removed in a future version. To use the new parser, pass option { useNewUrl
Parser: true } to MongoClient.connect.
events.js:183
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE :::3000
at Server.setupListenHandle [as _listen2] (net.js:1360:14)
at listenInCluster (net.js:1401:12)
at Server.listen (net.js:1485:7)
at Function.listen (C:\apiproject\bookstore\node_modules\express\lib\applica
tion.js:618:24)
at Object.<anonymous> (C:\apiproject\bookstore\app.js:14:5)
at Module._compile (module.js:653:30)
at Object.Module._extensions..js (module.js:664:10)
at Module.load (module.js:566:32)
at tryModuleLoad (module.js:506:12)
at Function.Module._load (module.js:498:3)
at Function.Module.runMain (module.js:694:10)
at startup (bootstrap_node.js:204:16)
at bootstrap_node.js:625:3
The error is showing that port 3000 is already in use.
Please get the list of all the ports in use and then kill the port 3000 and run the application again
netstat -a -o to get all the running ports
And then
Taskkill /PID -f (PID)
I have an application I attempting to connect to an mLab environment using Mongoose. I have the following code configured to attempt to connect to an mLab MongoDB Instance.
mongoose.connect(process.env.MONGODB_URI);
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', () => {});
The variable process.env.MONGODB_URI is equal to
mongodb://user:password#ds123456.mlab.com:12345/someRandomName (identifiers psuedonymized for privacy).
Applicable Software Versions
Node: v10.7.0
NPM: 6.2.0
Mongoose: 5.2.6
When I try to run the application I get the following error.
node ./bin/www
(node:2555) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
connection error: Error: Slash in host identifier
at parseConnectionString (/Users/nathanielsuchy/Documents/control-panel/node_modules/mongoose/node_modules/mongodb/lib/url_parser.js:219:15)
at parseHandler (/Users/nathanielsuchy/Documents/control-panel/node_modules/mongoose/node_modules/mongodb/lib/url_parser.js:129:14)
at module.exports (/Users/nathanielsuchy/Documents/control-panel/node_modules/mongoose/node_modules/mongodb/lib/url_parser.js:25:12)
at deprecated (internal/util.js:70:15)
at connect (/Users/nathanielsuchy/Documents/control-panel/node_modules/mongoose/node_modules/mongodb/lib/operations/mongo_client_ops.js:179:3)
at connectOp (/Users/nathanielsuchy/Documents/control-panel/node_modules/mongoose/node_modules/mongodb/lib/operations/mongo_client_ops.js:283:3)
at executeOperation (/Users/nathanielsuchy/Documents/control-panel/node_modules/mongoose/node_modules/mongodb/lib/utils.js:420:24)
at MongoClient.connect (/Users/nathanielsuchy/Documents/control-panel/node_modules/mongoose/node_modules/mongodb/lib/mongo_client.js:168:10)
at Promise (/Users/nathanielsuchy/Documents/control-panel/node_modules/mongoose/lib/connection.js:493:12)
at new Promise (<anonymous>)
at NativeConnection.Connection.openUri (/Users/nathanielsuchy/Documents/control-panel/node_modules/mongoose/lib/connection.js:490:19)
at Mongoose.connect (/Users/nathanielsuchy/Documents/control-panel/node_modules/mongoose/lib/index.js:230:15)
at Object.<anonymous> (/Users/nathanielsuchy/Documents/control-panel/app.js:20: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)
I am lead to two conclusions. One I need to use the new URL parser, second there's an issue I'm not seeing in my database connection string format. How should I proceed to fix the issue?
The correct format is mongodb://[username:password#]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]
Does your password contain the '#' or other special chars? If so:
const dbUrl = `mongodb://adminUsername:${encodeURIComponent('adminPassword')}#localhost:27017/mydb`;
Also check this: MongoDB password with "#" in it
I'm trying to make a connection to MongoDB using Mongoose. But it does neither throw a error nor connect to the database. Following is my code.
const express = require('express');
const app = express();
const port = process.env.PORT || 8080;
const mongoose = require('mongoose');
console.log('Hi, there!!');
mongoose.connect('mongodb://localhost:27017/db_name', (err) => {
console.log('Callback called');
if(err) throw err;
console.log('Connected to database');
})
In the above code none of the console.log inside the callback do happen. But any place outside the mongoose.connect do work like console.log('Hi, there!!')
Versions Used
express: 4.0.0
mongoose: 3.8.40
node: 7.7.3
mongodb: 3.4.0
Using mongoose: 3.8.40 I got this in the console :
{ Error: Cannot find module '../build/Release/bson'
at Function.Module._resolveFilename (module.js:470:15)
at Function.Module._load (module.js:418:25)
at Module.require (module.js:498:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/Users/kevin/nemeacreation/sites/test/stackoverflow/node_modules/bson/ext/index.js:15:10)
at Module._compile (module.js:571:32)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3) code: 'MODULE_NOT_FOUND' }
js-bson: Failed to load c++ bson extension, using pure JS version
Hi, there!!
upgrading to "mongoose": "~4.4" fixed it for me. I got the answer here : https://stackoverflow.com/a/35516644/2829540
For info the latest release of mongoose is 4.10.4
I'm new in using full stack to build an application, i tried to create a connection that allow to the client side to connect to my mongo database. here my server.js code file
console.log("Server running...!");
var mongo=require('mongodb').MongoClien,
client=require('socket.io').listen(8080).sockets;
mongo.connect('localhost:27017/chat',function(err,db){
if(err) throw err;
client.on('connection',function(socket){
console.log('someone has connected !');
//waiting for input
socket.on('input',function(data){
console.log(data);
});
});
});
when i run this with node.js i get the following error in my command prompt window.
Server running...!
C:\Users\azus\Desktop\Psirt\code-master\server.js:6
mongo.connect('mongodb://127.0.0.1:27017/chat',function(err,db){
^
TypeError: Cannot read property 'connect' of undefined
at Object.<anonymous> (C:\Users\azus\Desktop\Psirt\code-master\server.js:6:7
)
at Module._compile (module.js:413:34)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Function.Module.runMain (module.js:447:10)
at startup (node.js:139:18)
at node.js:999:3
I checked my mongo server and it's running !!
I am working with the latest up to date version, i'm doing this on windows.
'MongoClien' is unknown
You are trying to end a line with a comma
Change
var mongo=require('mongodb').MongoClien,
to:
var mongo = require('mongodb').MongoClient;