1

I am trying to create a web-application with node.js, mongoose and MongoDB,
I am trying to load the web-page localhost:8800/api/auth/register which is stuck at loading since past 15 minutes.

VS Code Terminal return the following :

(node:2908) UnhandledPromiseRejectionWarning: MongooseError: Operation `users.insertOne()` buffering timed out after 10000ms
    at Timeout.<anonymous> (E:\Projects\Applications\chitter-chatter\node_modules\mongoose\lib\drivers\node-mongodb-native\collection.js:185:20)
    at listOnTimeout (internal/timers.js:554:17)
    at processTimers (internal/timers.js:497:7)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:2908) 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: 3)
(node:2908) [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.
(node:2908) UnhandledPromiseRejectionWarning: MongooseError: Operation `users.insertOne()` buffering timed out after 10000ms
    at Timeout.<anonymous> (E:\Projects\Applications\chitter-chatter\node_modules\mongoose\lib\drivers\node-mongodb-native\collection.js:185:20)
    at listOnTimeout (internal/timers.js:554:17)
    at processTimers (internal/timers.js:497:7)
(node:2908) 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: 4)

My Scripts are as following :

  1. Index.js :

    const application = express();
    const mongoose = require("mongoose");
    const dotenv = require("dotenv");
    const helmet = require("helmet");
    const morgan = require("morgan");
    const userRoute = require("./routes/users");
    const authRoute = require("./routes/auth");
    
    dotenv.config();
    
    mongoose.connect(process.env.MONGO_URL, {useNewUrlParser: true, useUnifiedTopology: true}, () => {
        console.log("connected to MongoDB")
    });
    
    // middleware 
    application.use(express.json());
    application.use(helmet());
    application.use(morgan("common"));
    
    application.use("/api/users", userRoute);
    application.use("/api/auth", authRoute);
    
    application.listen(8800, () => {
        console.log("backend server is running!")
    })
    
    
  2. Auth.js :

    const User = require("../models/User");
    
    // REGISTER
    router.get("/register", async (req, res) => {
        const user = await new User ({
            username: "john",
            useremail: "john@gmail.com",
            userpswrd: "123456"
        })
    
        await user.save();
        res.send("oK")
    });
    
    module.exports = router
    
    

I am also using .env for MONGO VIA URL CONNECTION Sorry for the bad writing apologies in advance also I am new to this so pls correct me! I know i have done a lot of mistakes, Thanks for u're sincere time dedication and sympathy

2 Answers2

0

First of all, you must make sure that you are connecting to the database without any error.
To do this, start listening on connect's callback function:

try {
  // This configuration is better
  mongoose.connect(process.env.MONGO_URL, {
    useUnifiedTopology: true,
    useNewUrlParser: true,
    useCreateIndex: true,
    useFindAndModify: false,
  }, err => {
    if (err) throw Error(err.message);

    console.log("connected to MongoDB");
    application.listen(8800, () => console.log("backend server is running!"));
  });
} catch (error) {
  console.log(error);
}

I think you should make this operation in a try-catch statement like this:

// REGISTER
router.get("/register", async (req, res) => {
  try {
    // Create user in database
    const user = await User.create({
      username: "john",
      useremail: "john@gmail.com",
      userpswrd: "123456"
    });
    
    res.status(200).json({ success: true });
  } catch (error) {
    res.status(500).json({ error });
    console.log(error);
  }

});

module.exports = router;

Then you can see error's details, and server keeps running.
If you can't solve the problem just add a comment here I'll be back ASAP

0

check your router or wifi if you are using to run mongoose try with your mobile internet

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 21 '22 at 21:51