MongoDB object error

Block_MC

Honorary Poster
Hello po! im new with mongodb and grabe pati si ᑕᕼᗩTGᑭT di maka gets lol
anyways, can anyone help pano ma fix to? basically im posting a data in json format using postman pero nagkaka error ako pag i resubmit ko yug same data/json. Nagkaka error ako sa object (Patient).

heres my appointment.model.js:

Code:
import mongoose from 'mongoose';

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

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

export default Appointment;



tapos eto yung backend ko pag gumawa ng appointment (backend/appointment/create)

JavaScript:
// 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);
  }
};
 
gawa ka na lang new model ng patient tapos sa appointment patientId na lang lagay mu. populate mu na lang para makuha din data.

yung controller mo
Create -> patient
Get -> patientId
Create -> appointment w/ patient ID
Get -> appointment.populate(patient)
 

About this Thread

  • 1
    Replies
  • 409
    Views
  • 2
    Participants
Last reply from:
pusangitim13

Trending Topics

Online now

Members online
949
Guests online
1,721
Total visitors
2,670

Forum statistics

Threads
2,274,185
Posts
28,954,103
Members
1,234,130
Latest member
chestere
Back
Top