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 :
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!") })
Auth.js :
const User = require("../models/User"); // REGISTER router.get("/register", async (req, res) => { const user = await new User ({ username: "john", useremail: "[email protected]", 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
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:
I think you should make this operation in a try-catch statement like this:
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