This is a simple todo list I was doing and whenever I run it and reload the page it throws me an error. The code is :
import mongoose from "mongoose"
import express from "express"
import { Todo } from "./models/Todo.js"
let conn = mongoose.connect("mongodb://localhost:27017/todo")
const app = express()
const port = 3000
app.get('/', (req, res) => {
const todo = new Todo({ title: "First List", desc: "Description of my First List", isDone: false })
todo.save()
res.send('Hello World!')
})
app.listen(port, () => {
console.log(`Example app listening on port http://localhost:${port}`)
})
The error is :
MongooseError: Operation `todos.insertOne()` buffering timed out after 10000ms
In case anyone wants to see the TodoSchema :
import mongoose from "mongoose";
const todoSchema = new mongoose.Schema({
title: String,
desc: String,
isDone: Boolean
})
export const Todo = mongoose.model('Todo', todoSchema)
Instead of this what I want to see is a folder called todo gets inserted in my MongoDB Database in localhost with each time I reload the page based on the todoSchema.
2
Answers
There is a common source of confusion.
Please refer to the same incident and its solution.
Error: MongooseError: Operation
tours.insertOne()
buffering timed out after 10000ms at TimeoutThanks
WeDoTheBest4You
Essentially, you are trying to make database queries before you have established a connection.
Ideally you should wait for the connection and catch any errors to see if there are issues when connecting like so:
Then in your route handler you need to adopt an asynchronous approach to making CRUD operations with either
then/catch
blocks or use theasync/await
pattern like so: