Reputation: 41
I am new to MERN development and I have been trying to add the username, email and password of a user to database but i get this error(when i try to add user after connecting to the database from thunderclient in json format in vscode):
[nodemon] 2.0.19
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node ./index.js`
Example app listening on port 3000
connected to mongodb successfully
{ name: 'kashish', email: '[email protected]', password: '1234' }
/home/alien/codes/webDev/react/inotebook/backend/node_modules/mongoose/lib/drivers/node-mongodb-native/collection.js:151
const err = new MongooseError(message);
^
MongooseError: Operation `users.insertOne()` buffering timed out after 10000ms
at Timeout.<anonymous> (/home/alien/codes/webDev/react/inotebook/backend/node_modules/mongoose/lib/drivers/node-mongodb-native/collection.js:151:23)
at listOnTimeout (node:internal/timers:564:17)
at process.processTimers (node:internal/timers:507:7)
Node.js v18.7.0
[nodemon] app crashed - waiting for file changes before starting...
The code connects to the database successfully, but when i try to add the user (using thunder client before calling the api) it just shows me the given error here is the code which connects to the database and the one which saves data in database respectively:
const mongoose=require("mongoose")
const mongoUri="mongodb://localhost:27017"
const connectMongo=() => {
mongoose.connect(mongoUri,() => {
console.log("connected to mongodb successfully");
}).catch(error => handleError(error))
}
module.exports=connectMongo;
the code that adds user:
const express=require('express');
const User=require('../models/User');
const router=express.Router();
router.get('/',(req,res) => {
console.log(req.body);
const user=User(req.body);
user.save()
res.send(req.body);
})
module.exports=router;
I tried going through similar issues from the forum but i don't understand any of them, what am i missing?
another edit: When i tried using async function and added await before user.save(), the response didn't occur, not sure if that is obvious but the user data shows up in console but there happens to be an error during saving data, is it possible that the timeout happens before the data can be saved? i am hosting mongodb on localhost so this is not an internet issue
here is the updated code for auth.js:
const express=require('express');
const User=require('../models/User');
const router=express.Router();
router.get('/',async (req,res) => {
console.log(req.body);
const user=User(req.body);
await user.save()
res.send(req.body);
})
module.exports=router;
updated code of db.js (connecting to database) with error handling:
const mongoose=require("mongoose")
const mongoUri="mongodb://localhost:27017"
const connectMongo=() => {
mongoose.connect(mongoUri).then(
() => console.log('connected')
).catch(e => console.log('error',e))
}
module.exports=connectMongo;
after the above update in code this shows up in a few mins:
Example app listening on port 3000
error MongooseServerSelectionError: connect ECONNREFUSED ::1:27017
at Connection.openUri (/home/alien/codes/webDev/react/inotebook/backend/node_modules/mongoose/lib/connection.js:824:32)
at /home/alien/codes/webDev/react/inotebook/backend/node_modules/mongoose/lib/index.js:380:10
at /home/alien/codes/webDev/react/inotebook/backend/node_modules/mongoose/lib/helpers/promiseOrCallback.js:41:5
at new Promise (<anonymous>)
at promiseOrCallback (/home/alien/codes/webDev/react/inotebook/backend/node_modules/mongoose/lib/helpers/promiseOrCallback.js:40:10)
at Mongoose._promiseOrCallback (/home/alien/codes/webDev/react/inotebook/backend/node_modules/mongoose/lib/index.js:1225:10)
at Mongoose.connect (/home/alien/codes/webDev/react/inotebook/backend/node_modules/mongoose/lib/index.js:379:20)
at connectMongo (/home/alien/codes/webDev/react/inotebook/backend/db.js:5:14)
at Object.<anonymous> (/home/alien/codes/webDev/react/inotebook/backend/index.js:3:1)
at Module._compile (node:internal/modules/cjs/loader:1120:14) {
reason: TopologyDescription {
type: 'Unknown',
servers: Map(1) { 'localhost:27017' => [ServerDescription] },
stale: false,
compatible: true,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
logicalSessionTimeoutMinutes: undefined
},
code: undefined
}
and before you ask, i already ran sudo systemctl start mongodb
before starting the app. here is the output of sudo systemctl status mongodb
:
● mongodb.service - MongoDB Database Server
Loaded: loaded (/usr/lib/systemd/system/mongodb.service; disabled; preset: disabled)
Active: active (running) since Mon 2022-08-22 14:59:01 IST; 24s ago
Docs: https://docs.mongodb.org/manual
Main PID: 5840 (mongod)
Memory: 163.0M
CPU: 857ms
CGroup: /system.slice/mongodb.service
└─5840 /usr/bin/mongod --config /etc/mongodb.conf
Upvotes: 0
Views: 835
Reputation: 203231
error MongooseServerSelectionError: connect ECONNREFUSED ::1:27017
This issue can occur when the MongoDB client is trying to connect to the MongoDB server over IPv6, but the server isn't actually listening on IPv6 (for whatever reason; I think that older versions of the server had to be told to listen on IPv6 but more recent versions should do so automatically, unless it's explicitly turned off).
To force the client to connect over IPv4, use the IPv4 localhost address:
const mongoUri="mongodb://127.0.0.1:27017"
Upvotes: 3