skip to Main Content

sorry if the title doesnt fit to the problem.

My question is how do I implement a post method with data and obeject? at first submit, i can create an appointment but when I resubmit the data from insomnia, i get an error message

{
    "success": false,
    "message": "E11000 duplicate key error collection: MCityVet.appointments index: patientId_1 dup key: { patientId: null }",
    "statusCode": 500
}

unless if i delete to the database, i can create new appointment but still if i resubmit, i get an error.

heres my appointment model:

import mongoose from 'mongoose';

const appointmentSchema = new mongoose.Schema({
    schedule: {
        type: String,
    },
    technicianName: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User',
    },
    firstName: {
        type: String,
    },
    lastName: {
        type: String,
    },
    phone: {
        type: String,
    },
    patient: {
        typeOfAnimal: {
            type: String,
        },
        numberOfHeads: {
            type: Number,
        },
        age: {
            type: Number,
        },
        services: [{
            type: String,
        }],
        address: {
            type: String,
        },
        landmark: {
            type: String,
        },
    },
});

const Appointment = mongoose.model('Appointment', appointmentSchema);
export default Appointment;

heres my cotroller (backend/appointment/create) :

// Create Appointment
export const createAppointment = async (req, res, next) => {
  try {
    const { schedule, technicianName, firstName, lastName, phone, patient } = req.body;

    // Create a new appointment instance
    const newAppointment = new Appointment({
      schedule,
      technicianName,
      firstName,
      lastName,
      phone,
      patient,
    });

    // Save the appointment to the database
    const savedAppointment = await newAppointment.save();

    res.status(201).json(savedAppointment);
  } catch (error) {
    next(error);
  }
};

i also tried the uuid but still got a duplication/null error. heres a dummy json data:

{
  "schedule": "2023-11-15",
  "technicianName": "653f74dee45212eac760a17d",
  "firstName": "John",
  "lastName": "Doe",
  "phone": "123-456-7890",
  "patient": {
    "typeOfAnimal": "Cat",
    "numberOfHeads": 2,
    "age": 2,
    "services": ["Grooming"],
    "address": "1234 Elm",
    "landmark": "Near park"
  }
}

2

Answers


  1. Add an Id to increment. I think the error is a server side error, because it can’t upload the Id and take it to null, but if you resend your data, the database try to set an id to null and crash

    Login or Signup to reply.
  2. The error is calling out the field ‘patientId’ in the collection ‘MCityVet.appointments’, but there is no field with that name in the schema.

    Mongodb enforces uniqueness using indexes, so this means the database has an index on a field that doesn’t exist.

    This might have been leftover from a previous test or version.

    To fix this, drop the ‘patientId_1’ index from the database.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search