Click here to Skip to main content
15,887,464 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
patientmodel.js
const mongoose = require("mongoose");

const patientSchema = new mongoose.Schema({
  name: {
    type: String,
    required: true,
  },
  age: {
    type: Number,
    required: true,
  },
  gender: {
    type: String,
    required: true,
  },
  occupation: {
    type: String,
    required: true,
  },
  phone: {
    type: String,
    required: true,
  },
  address: {
    type: String,
    required: true,
  },
  complaints: {
    type: String,
    required: true,
  },
  history: {
    type: String,
    required: true,
  },
  allergies: {
    type: String,
    required: true,
  },
  general_examination: {
    type: String,    required: true,
  },
  level_of_consciousness: {
    type: String,
    required: true,
  },
  vitals: {
    pulse: {
      type: Number,
      required: true,
    },
    bloodPressure: {
      systolic: {
        type: Number,
        required: true,
      },
      diastolic: {
        type: Number,
        required: true,
      },
    },
    temperature: {
      type: Number,
      required: true,
    },
    oxygensaturation: {
      type: Number,
      required: true,
    },
    bsl: {
      type: Number,
      required: true,
    },
  },
  systematicexam: {
    cvs: {
      type: String,
      required: true,
    },
    rs: {
      type: String,
      required: true,
    },
    cns: {
      type: String,
      required: true,
    },
    pa: {
      type: String,
      required: true,
    },
  },
  local_examination: {
    type: String,
    required: true,
  },
  treatmentPlan: {
    investadvised: {
      type: String,
      required: true,
    },
    medication: {
      type: String,
      required: true,
    },
    specialNeeds: {
      type: String,
      required: true,
    },
  },
  assessedBy: {
    doctorName: {
      type: String,
      required: true,
    },
    date: {
      type: Date,
      required: true,
    },
    time: {
      type: String,
      required: true,
    },
  },
  assistedBy: {
    doctorName: {
      type: String,
      required: true,
    },
    date: {
      type: Date,
      required: true,
    },
    time: {
      type: String,
      required: true,
    },
  },
});

const Patient = mongoose.model("Patient", patientSchema);
module.exports = Patient;



patient.js
import React, { useState } from "react";
import { useNavigate } from "react-router-dom";
import "./Patient.css";
function Patient() {
  const navigate = useNavigate();
  const [patientData, setPatientData] = useState({
    name: "",
    age: "",
    gender: "",
    occupation: "",
    phone: "",
    address: "",
    complaints: "",
    history: "",
    allergies: "",
    general_examination: "",
    level_of_consciousness: "",
    vitals: {
      pulse: null,
      bloodPressure: {
        systolic: null,
        diastolic: null,
      },
      temperature: null,
      oxygenSaturation: null,
      bsl: null,
    },
    systematicexam: {
      cvs: null,
      rs: null,
      cns: null,
      pa: null,
    },
    local_examination: "",
    treatmentPlan: {
      investadvised: null,
      medication: null,
      specialNeeds: null,
    },
    assessedBy: {
      doctorName: null,
      date: null,
      time: null,
    },
    assistedBy: {
      doctorName1: null,
      date1: null,
      time1: null,
    },
  });
  const [errorMessage, setErrorMessage] = useState("");
  const [isSubmitted, setIsSubmitted] = useState(false);

  /*const handleInputChange = (event) => {
    const { name, value } = event.target;
    setPatientData({ ...patientData, [name]: value });
  };
*/
/*const handleInputChange = (event) => {
  const { name, value } = event.target;
  const nameArray = name.split('.');
  let newData = { ...patientData };

  nameArray.reduce((acc, curr, index) => {
    if (index === nameArray.length - 1) {
      acc[curr] = value;
    } else {
      if (!acc[curr]) {
        acc[curr] = isNaN(nameArray[index + 1]) ? {} : [];
      }
    }
    return acc[curr];
  }, newData);

  setPatientData(newData);
};
*/const handleInputChange = (event) => {
  const { name, value } = event.target;
  const nameArray = name.split('.');
  let newData = { ...patientData };

  nameArray.reduce((acc, curr, index) => {
    if (index === nameArray.length - 1) {
      acc[curr] = value;
    } else {
      if (!acc[curr]) {
        acc[curr] = isNaN(nameArray[index + 1]) ? {} : [];
      } else if (!Array.isArray(acc[curr]) && typeof acc[curr] !== 'object') {
        // If acc[curr] is not an array or object, then it cannot contain properties
        // accessed by nameArray[index + 1]. Return acc to stop further iteration.
        return acc;
      }
    }
    if (acc[curr] === undefined) {
      // Initialize acc[curr] as an empty object or array as appropriate
      acc[curr] = isNaN(nameArray[index + 1]) ? {} : [];
    }
    return acc[curr];
  }, newData);

  setPatientData(newData);
};

  const handleSubmit = async (event) => {
    event.preventDefault();
    if (
      !patientData.name ||
      !patientData.age ||
      !patientData.gender ||
      !patientData.address ||
      !patientData.phone ||
      !patientData.occupation ||
      !patientData.history ||
      !patientData.complaints ||
      !patientData.allergies ||
      !patientData.vitals.pulse ||
      !patientData.vitals.bloodPressure.systolic ||
      !patientData.vitals.bloodPressure.diastolic ||
      !patientData.vitals.temperature ||
      !patientData.vitals.oxygenSaturation ||
      !patientData.vitals.bsl ||
      !patientData.systematicexam.cvs ||
      !patientData.systematicexam.rs ||
      !patientData.systematicexam.cns ||
      !patientData.systematicexam.pa ||
      !patientData.local_examination
    ) {
      let errorMessage = "Please fill in all the required fields:";
      if (!patientData.name) errorMessage += " Name";
      if (!patientData.age) errorMessage += " Age";
      if (!patientData.gender) errorMessage += " Gender";
      if (!patientData.address) errorMessage += " Address";
      if (!patientData.phone) errorMessage += " Phone";
      if (!patientData.occupation) errorMessage += " Occupation";
      if (!patientData.history) errorMessage += " History";
      if (!patientData.complaints) errorMessage += " Complaints";
      if (!patientData.allergies) errorMessage += " Allergies";
      if (!patientData.vitals.pulse) errorMessage += " Pulse";
      if (!patientData.vitals.bloodPressure.systolic) errorMessage += " Systolic Blood Pressure";
      if (!patientData.vitals.bloodPressure.diastolic) errorMessage += " Diastolic Blood Pressure";
      if (!patientData.vitals.temperature) errorMessage += " Temperature";
      if (!patientData.vitals.oxygenSaturation) errorMessage += " Oxygen";
      if (!patientData.vitals.bsl) errorMessage += " Blood Sugar Level";
      if (!patientData.systematicexam.cvs) errorMessage += " CVS";
      if (!patientData.systematicexam.rs) errorMessage += " RS";
      if (!patientData.systematicexam.cns) errorMessage += " CNS";
      if (!patientData.systematicexam.pa) errorMessage += " PA";
      if (!patientData.local_examination) errorMessage += " Local Examination";
      setErrorMessage(errorMessage);
      return;
    }
    

    try {
      const response = await fetch("/api/admin/register-patient", {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
          Authorization: `Bearer ${localStorage.getItem("token")}`,
        },
        body: JSON.stringify(patientData),
      });

      if (!response.ok) {
        throw new Error("Network response was not ok");
      }

      setIsSubmitted(true);
      navigate.push("/");
      // TODO: handle successful submission
    } catch (error) {
      console.log(error);
      setErrorMessage("Something went wrong. Please try again later.");
    }
  };

  if (isSubmitted) {
    return (
      <div>
        <p>Form successfully submitted!</p>
      </div>
    );
  }

  return (
    <form onSubmit={handleSubmit}>
      {errorMessage && <div style={{ color: "red" }}>{errorMessage}</div>}
      <label>
        Name:
        <input
          type="text"
          name="name"
          value={patientData.name}
          onChange={handleInputChange}
        />
      </label>

      <label>
        Age:
        <input
          type="number"
          name="age"
          value={patientData.age}
          onChange={handleInputChange}
        />
      </label>

      <label>
        Gender:
        <select
          name="gender"
          value={patientData.gender}
          onChange={handleInputChange}
        >
          <option value="">Select gender</option>
          <option value="male">Male</option>
          <option value="female">Female</option>
          <option value="other">Other</option>
        </select>
      </label>
      <label>
        Occupation:
        <textarea
          name="occupation"
          value={patientData.occupation}
          onChange={handleInputChange}
        ></textarea>
      </label>

      <label>
        Address:
        <textarea
          name="address"
          value={patientData.address}
          onChange={handleInputChange}
        ></textarea>
      </label>

      <label>
        Phone:
        <input
          type="text"
          name="phone"
          value={patientData.phone}
          onChange={handleInputChange}
        />
      </label>
      <label>
        complaints:
        <textarea
          name="complaints"
          value={patientData.complaints}
          onChange={handleInputChange}
        ></textarea>
      </label>

      <label>
        Medical history:
        <textarea
          name="history"
          value={patientData.history}
          onChange={handleInputChange}
        ></textarea>
      </label>
      <label>
        allergies:
        <textarea
          name="allergies"
          value={patientData.allergies}
          onChange={handleInputChange}
        ></textarea>
      </label>
      <label>
        Level of Conciousness:
        <textarea
          name="level_of_conciousness"
          value={patientData.level_of_conciousness}
          onChange={handleInputChange}
        ></textarea>
      </label>
      <label>
        General Examination:
        <textarea
          name="general_examination"
          value={patientData.general_examination}
          onChange={handleInputChange}
        ></textarea>
      </label>

      <label>
        <table>
          <thead>
            <tr>
              <th>Vital Parameters</th>
              <th>Values</th>
            </tr>
          </thead>
          <tbody>
            <tr>
              <td>Pulse</td>
              <td>
                <input
                  type="number"
                  name="vitals.pulse"
                  value={patientData.vitals.pulse}
                  onChange={handleInputChange}
                />
              </td>
            </tr>
            <tr>
              <td>Blood Pressure (systolic)</td>
              <td>
                <input
                  type="number"
                  name="vitals.bloodPressure.systolic"
                  value={patientData.vitals.bloodPressure.systolic}
                  onChange={handleInputChange}
                />
              </td>
            </tr>
            <tr>
              <td>Blood Pressure (diastolic)</td>
              <td>
                <input
                  type="number"
                  name="vitals.bloodPressure.diastolic"
                  value={patientData.vitals.bloodPressure.diastolic}
                  onChange={handleInputChange}
                />
              </td>
            </tr>
            <tr>
              <td>Temperature</td>
              <td>
                <input
                  type="number"
                  name="vitals.temperature"
                  value={patientData.vitals.temperature}
                  onChange={handleInputChange}
                />
              </td>
            </tr>
            <tr>
              <td>Oxygen saturation</td>
              <td>
                <input
                  type="number"
                  name="vitals.oxygenSaturation"
                  value={patientData.vitals.oxygenSaturation}
                  onChange={handleInputChange}
                />
              </td>
            </tr>
            <tr>
              <td>BSL (Blood Sugar Level)</td>
              <td>
                <input
                  type="number"
                  name="vitals.bsl"
                  value={patientData.vitals.bsl}
                  onChange={handleInputChange}
                />
              </td>
            </tr>
          </tbody>
        </table>
      </label>

      <label>
        <table>
          <thead>
            <tr>
              <th>Systematic Examination</th>
              <th>Findings</th>
            </tr>
          </thead>
          <tbody>
            <tr>
              <td>Cardiovascular System (CVS)</td>
              <td>
                <textarea
                  name="systematicexam.cvs"
                  value={patientData.systematicexam.cvs}
                  onChange={handleInputChange}
                ></textarea>
              </td>
            </tr>
            <tr>
              <td>Respiratory System (RS)</td>
              <td>
                <textarea
                  name="systematicexam.rs"
                  value={patientData.systematicexam.rs}
                  onChange={handleInputChange}
                ></textarea>
              </td>
            </tr>
            <tr>
              <td>Central Nervous System (CNS)</td>
              <td>
                <textarea
                  name="systematicexam.cns"
                  value={patientData.systematicexam.cns}
                  onChange={handleInputChange}
                ></textarea>
              </td>
            </tr>
            <tr>
              <td>Per Abdomen (PA)</td>
              <td>
                <textarea
                  name="systematicexam.pa"
                  value={patientData.systematicexam.pa}
                  onChange={handleInputChange}
                ></textarea>
              </td>
            </tr>
          </tbody>
        </table>
      </label>
      <label>
        Local Examination:
        <textarea
          name="local_examination"
          value={patientData.local_examination}
          onChange={handleInputChange}
        ></textarea>
      </label>
      <label>
        Investigations advised:
        <textarea
          name="treatmentPlan.investadvised"
          value={patientData.treatmentPlan.investadvised}
          onChange={handleInputChange}
        ></textarea>
      </label>
      <label>
        Medication as per order form:
        <textarea
          name="treatmentPlan.medication"
          value={patientData.treatmentPlan.medication}
          onChange={handleInputChange}
        ></textarea>
      </label>
      <label>
        Special needs:
        <textarea
          name="treatmentPlan.specialNeeds"
          value={patientData.treatmentPlan.specialNeeds}
          onChange={handleInputChange}
        ></textarea>
      </label>
      <label>
        <table>
          <thead>
            <tr>
              <th></th>
              <th>Doctor Name</th>
              <th>Date</th>
              <th>Time</th>
            </tr>
          </thead>
          <tbody>
            <tr>
              <td>Assessed By</td>
              <td>
                <input
                  type="text"
                  name="assessedBy.doctorName"
                  value={patientData.assessedBy.doctorName}
                  onChange={handleInputChange}
                />
              </td>
              <td>
                <input
                  type="date"
                  name="assessedBy.date"
                  value={patientData.assessedBy.date}
                  onChange={handleInputChange}
                />
              </td>
              <td>
                <input
                  type="time"
                  name="assessedBy.time"
                  value={patientData.assessedBy.time}
                  onChange={handleInputChange}
                />
              </td>
            </tr>
            <td>Assisted By</td>
            <td>
              <input
                type="text"
                name="assistedby.doctorName"
                value={patientData.assistedBy.doctorName1}
                onChange={handleInputChange}
              />
            </td>
            <td>
              <input
                type="date"
                name="assistedby.date"
                value={patientData.assistedBy.date1}
                onChange={handleInputChange}
              />
            </td>
            <td>
              <input
                type="time"
                name="assistedby.time"
                value={patientData.assistedBy.time1}
                onChange={handleInputChange}
              />
            </td>
            <tr>
              <td></td>
              <td></td>
            </tr>
          </tbody>
        </table>
      </label>

      <button type="submit">Submit</button>
    </form>
  );
}

export default Patient;
I am getting errors like properties: [Object],
kind: 'required',
path: 'general_examination',
value: undefined,
reason: undefined,
[Symbol(mongoose:validatorError)]: true
},
allergies: ValidatorError: Path `allergies` is required.
at validate (C:\Users\bhide\Desktop\MP\MP3\SEVASADAN\node_modules\mongoose\lib\schematype.js:1346:13)
at SchemaString.SchemaType.doValidate (C:\Users\bhide\Desktop\MP\MP3\SEVASADAN\node_modules\mongoose\lib\schematype.js:1330:7)
at C:\Users\bhide\Desktop\MP\MP3\SEVASADAN\node_modules\mongoose\lib\document.js:2933:18
at processTicksAndRejections (node:internal/process/task_queues:78:11) {
properties: [Object],
kind: 'required',
path: 'allergies',
value: undefined,
reason: undefined,
[Symbol(mongoose:validatorError)]: true
},
history: ValidatorError: Path `history` is required.
at validate (C:\Users\bhide\Desktop\MP\MP3\SEVASADAN\node_modules\mongoose\lib\schematype.js:1346:13)
at SchemaString.SchemaType.doValidate (C:\Users\bhide\Desktop\MP\MP3\SEVASADAN\node_modules\mongoose\lib\schematype.js:1330:7)
at C:\Users\bhide\Desktop\MP\MP3\SEVASADAN\node_modules\mongoose\lib\document.js:2933:18
at processTicksAndRejections (node:internal/process/task_queues:78:11) {
properties: [Object],
kind: 'required',
path: 'history',
value: undefined,
reason: undefined,
[Symbol(mongoose:validatorError)]: true
},
complaints: ValidatorError: Path `complaints` is required.
at validate (C:\Users\bhide\Desktop\MP\MP3\SEVASADAN\node_modules\mongoose\lib\schematype.js:1346:13)
at SchemaString.SchemaType.doValidate (C:\Users\bhide\Desktop\MP\MP3\SEVASADAN\node_modules\mongoose\lib\schematype.js:1330:7)
at C:\Users\bhide\Desktop\MP\MP3\SEVASADAN\node_modules\mongoose\lib\document.js:2933:18
at processTicksAndRejections (node:internal/process/task_queues:78:11) {
properties: [Object],
kind: 'required',
path: 'complaints',
value: undefined,
reason: undefined,
[Symbol(mongoose:validatorError)]: true
},
occupation: ValidatorError: Path `occupation` is required.
at validate (C:\Users\bhide\Desktop\MP\MP3\SEVASADAN\node_modules\mongoose\lib\schematype.js:1346:13)
at SchemaString.SchemaType.doValidate (C:\Users\bhide\Desktop\MP\MP3\SEVASADAN\node_modules\mongoose\lib\schematype.js:1330:7)
at C:\Users\bhide\Desktop\MP\MP3\SEVASADAN\node_modules\mongoose\lib\document.js:2933:18
at processTicksAndRejections (node:internal/process/task_queues:78:11) {
properties: [Object],
kind: 'required',
path: 'occupation',
value: undefined,
reason: undefined,
[Symbol(mongoose:validatorError)]: true
}
},
_message: 'Patient validation failed'

What I have tried:

I tried checking the validation of the fields is done right still I cant solve
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900