7

I am trying to create a project which allows the user to enter "email", "username" and "Password" to register to the site, When I try to enter a user using the "username", email" and "password" to enter this site, I get the following error:

Backend server is running
not connected
C:\Users\odewo\chat-app\NODE-REST-API\node_modules\mongoose\lib\drivers\node-mongodb-native\collection.js:149
          const err = new MongooseError(message);
                      ^

MongooseError: Operation `users.insertOne()` buffering timed out after 10000ms
    at Timeout.<anonymous> (C:\Users\odewo\chat-app\NODE-REST-API\node_modules\mongoose\lib\drivers\node-mongodb-native\collection.js:149:23)
    at listOnTimeout (node:internal/timers:557:17)
    at processTimers (node:internal/timers:500:7)

Below is my mongoose code:

mongoose.connect(process.env.MONGO_URL, {
  userNewUrlPaser: true,
  useUnifiedTopology: true,
  useCreateIndex: true,
})
  .then(() => {
    console.log('Connected to MongoDB');
  })
  .catch((e) => {
    console.log('not connected');
  });

This is auth.js code for routes:

const router = require('express').Router();
const User = require('../models/User');

//  REGISTER
router.get('/register', async (req, res) => {
  const user = await new User({
    username: 'samson',
    email: 'samson@gmail.com',
    password: '123456',
  });

  await user.save();
  res.send('ok');
});
module.exports = router;

I will really appreciate your help

Tunde Samson
  • 71
  • 1
  • 1
  • 4
  • With the below code, Check `mongoose` connection: `mongoose.connect(process.env.MONGO_URL, () => { console.log("connected to mongo..") }); ` – ParisaN Dec 23 '21 at 06:27

6 Answers6

3

Monogo Db Atlas -> Network Access -> IP Access List -> IP address updated to access from any where works for me.

Reason : My Internet provider allots dynamic IP and every time i login the IP Address changes and this throws error.

2

Check your MONGO_URL and make sure your password or username is not wrapped in < > tags. I think your code is fine but its obvious that there are some problems. I would be checking the .env file to make sure.

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 23 '22 at 11:50
1

The problem is not solved by removing the await. It is better to implement this function asynchronously. You must log in to your mongo account and add your IP address from the Network Access part.

enter image description here

ParisaN
  • 1,816
  • 2
  • 23
  • 55
0

you should remove the await before the new User declaration , use await just when you are waiting for the result of a promise like in await user.save()

  • Even after I did that, it still isn't connecting to MongoDB...... please help me to check the mongoose connection if there's any error in the code – Tunde Samson Sep 15 '21 at 00:34
0

Changing your url will solve this issue I have also faced this once. Put this mongoose.connect('mongodb://127.0.0.1:27017'); and your other code remains the same.

-1

You can solve this problem by using a simple password for your atlasDatabase meaning avoid using any special characters in your password like "@" or "$" and if you use then they must be url encoded that's why it is better if you avoid special characters and it worked for me and I hope it will work for you too.enter image description here

  • Advising to use weak security doesn't seem appropriate. – Maarten Bicknese Mar 24 '22 at 12:20
  • I didn't mean that you should'nt use special characters. I meant that if you use special characters in your password then they must be url encoded when you are using the connection string in your application otherwise your application would'nt make any collections in mongoAtlas. – Priyansh Gupta Mar 27 '22 at 08:58