What's new

MongoDB object error

Block_MC

Eternal Poster
Joined
Apr 29, 2018
Posts
351
Solutions
1
Reaction
64
Points
257
Hello po! im new with mongodb and grabe pati si chatGPT 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)
 

Similar threads

Back
Top