Quantcast
Channel: Active questions tagged mongodb-atlas - Stack Overflow
Viewing all articles
Browse latest Browse all 271

Attempt to delete a document on Mongodb Atlas times out and returns an error

$
0
0

I am using Postman to test deleting a document on Mongodb atlas in a NextJs 14.2.4 application using the api router. The problem is that my delete methods time out and the document is not deleted. This occurs with Employee.deleteOne({ _id: id }) and Employee.findByIdAndDelete(id). I have also tried the remove( ) method directly on the employee collection which also times out.

This is the route:

import { connectToMongoDB } from "@/app/lib/db";import Employee from "@/models/employee";import { NextResponse } from "next/server";import mongoose from "mongoose";export async function DELETE(request) {    const id = request.nextUrl.searchParams.get("id");    console.log('id: ', id);    console.log('id type: ', typeof id);    if (!id) {        return NextResponse.json({ message: "Employee ID is required" }, { status: 400 });    }    if (!mongoose.Types.ObjectId.isValid(id)) {        return NextResponse.json({ message: "Invalid employee ID" }, { status: 400 });    }    try {        await connectToMongoDB();        console.log("MongoDB connected successfully");        // const deletedEmployee = await Employee.findByIdAndDelete(id);       const deletedEmployee = await Employee.deleteOne({ _id: id });        if (deletedEmployee) {            return NextResponse.json({ message: "Employee deleted successfully" }, { status: 200 });        } else {            return NextResponse.json({ message: "Employee not found" }, { status: 404 });        }    } catch (error) {        console.log("Error during deletion:", error);        return NextResponse.json({ message: "An error occurred while deleting the employee" }, { status: 500 });    }}

This is my Employee Schema:

import mongoose from "mongoose";const contactSchema = new mongoose.Schema({  phone: {    type: String,    required: false,  },});const employeeAddressSchema = new mongoose.Schema({  street: {    type: String,    required: false,  },  city: {    type: String,    required: false,  },  state: {    type: String,    required: false,  },  zip: {    type: String,    required: false,  },});const geoSchema = new mongoose.Schema({  lat: {    type: Number,    required: false,  },  lng: {    type: Number,    required: false,  },});const assignmentAddressSchema = new mongoose.Schema({  street: {    type: String,    required: false,  },  city: {    type: String,    required: false,  },  state: {    type: String,    required: false,  },  zip: {    type: String,    required: false,  },});const EmployeeSchema = new mongoose.Schema({  firstName: {    type: String,    required: true,  },  lastName: {    type: String,    required: true,  },  email: {    type: String,    required: true,    unique: true,  },  isAdmin: {    type: Boolean,    default: false,  },  contact: {    type: contactSchema,    default: null,  },  employeeAddress: {    type: employeeAddressSchema,    default: null,  },  locationName: {    type: String,    required: false,  },  locationId: {    type: mongoose.Schema.Types.Mixed,    default: null,  },  geo: {    type: geoSchema,    default: null,  },  assignmentAddress : {    type: assignmentAddressSchema,    default: null,  },},{  timestamps: true});// Define the modelconst Employee = mongoose.models.Employee || mongoose.model('Employee', EmployeeSchema);export default Employee;

This is the request url:

localhost:3000/api/employees?id=66972dc9f332a2f854b2cdc5

I have ensured that:

  1. Mongodb connection is open
  2. The object id param is valid
  3. The object id passed in the url matches the _id of a document in my collection

Without fail, this is what is logged on the console:

id:  66972dc9f332a2f854b2cdc5id type:  stringConnecting to MongoDB...MongoDB connected successfullyMongoDB connected successfullyError during deletion: MongooseError: Operation `employees.findOneAndDelete()` buffering timed out after 10000ms    at Timeout.<anonymous> (/Users/imarichildress/Documents/dev-projects/portfolio-new-2024/employee-mapping-app/vscode/employee-map-dashboard/node_modules/mongoose/lib/drivers/node-mongodb-native/collection.js:185:23)    at listOnTimeout (node:internal/timers:573:17)    at process.processTimers (node:internal/timers:514:7) DELETE /api/employees?id=66972dc9f332a2f854b2cdc5 500 in 10869ms

This tells me that the client is communication with the server route. The failure seems to happening when I execute any of the delete methods.

I am sure the connection to my db is open because

  1. My GET route to retrieve all documents works.
  2. My POST route to create a new document works.

I have ensured that:

  1. Mongodb connection is open
  2. The object id param is valid
  3. The object id passed in the url matches the _id of a document in my collection

Viewing all articles
Browse latest Browse all 271

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>