0
votes

I was following a tutorial but got stuck at one point. I try to make a POST request in Postman, but an error is returned which I can't solve. The error is as below:

(node:3224) UnhandledPromiseRejectionWarning: MongooseError: Operation users.insertOne() buffering timed out after 10000ms at Timeout. (C:\Users\Murad\Desktop\Full Stack\Node\node_modules\mongoose\lib\drivers\node-mongodb-native\collection.js:185:20) at listOnTimeout (internal/timers.js:549:17) at processTimers (internal/timers.js:492:7) (Use node --trace-warnings ... to show where the warning was created) (node:3224) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1) (node:3224) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

The code is below

// index.js File
const express = require('express');
const app = express();
const mongoose = require('mongoose');
// const dotenv = require('dotenv');

// Import Routes
const authRoute = require('./routes/auth');

// Route middleware
app.use(express.json());
app.use('/api/user', authRoute);

// dotenv.config();
const myUrl = 'mongodb+srv://murad:<password>@cluster0.ag5vr.mongodb.net/DatabaseName?retryWrites=true&w=majority';

mongoose.connect(myUrl,
    { useNewUrlParser: true, useUnifiedTopology: true },
    () => {
        console.log('Connected to db');
    })



app.listen(3000, () => console.log('Server up and running'));

// My auth.js (router) file
const router = require('express').Router();
const User = require('../model/User');

router.post('/register', async (req, res) => {
    const user = new User({
        name: req.body.name,
        email: req.body.email,
        password: req.body.password
    })
    
        const savedUser = await user.save();
        res.send(savedUser);
    
})

module.exports = router;

// And my User.js (model) file
````const mongoose = require('mongoose');

const userSchema = mongoose.Schema({
    name: {
        type: String,
        required: true,
        min: 6,
        max: 255
    },
    email: {
        type: String,
        required: true,
        min: 6,
        max: 255
    },
    password: {
        type: String,
        required: true,
        min: 6,
        max: 1024
    },
    date: {
        type: Date,
        default: Date.now
    }
})

module.exports = mongoose.model('User', userSchema);

I tried to look up solutions but none seemed to work. Help would be much appreciated! Thanks in advance
2

2 Answers

0
votes
router.post('/register', async (req, res) => {
const user = new User({
    name: req.body.name,
    email: req.body.email,
    password: req.body.password
})
    const savedUser = await user.save().catch( e => { console.error(e) 
    res.send(savedUser);

});

try using a catch to catch the errors

0
votes
const savedUser = await user.save();

user.save() returns a promise but your not handling it

correct code would be

const savedUser = await user.save();
savedUser
.then((users)=>
{
  
},(err)=>next(err))
.catch((err)=>next(err))
})