JSON Body is Not Passing Certain Strings

51 views Asked by At
function sendDataToServer(
  firstName,
  lastName,
  phoneNumber,
  address,
  birthday,
  age,
  idNumber,
  gender,
  degree,
  intake,
  semester,
  course
) {
  console.log("sending date" + birthday);
  console.log("sending nic" + idNumber);
  console.log("sending phone" + phoneNumber);
  console.log(typeof phoneNumber);
  console.log(typeof idNumber);
  console.log(typeof birthday);
  fetch("http://localhost:8080/student/add", {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
    },
    body: JSON.stringify({
      firstName: firstName,
      lastName: lastName,
      phoneNumber: phoneNumber,
      address: address,
      birthday: birthday,
      age: age,
      idNumber: idNumber,
      gender: gender,
      degree: degree,
      intake: intake,
      semester: semester,
      course: course,
    }),
  })
    .then((response) => {
      if (response.ok) {
        alert("Student added successfully");
      } else {
        alert("An error occurred");
        console.log(response);
      }
    })
    .catch((error) => {
      console.log(error);
      alert("An error occurred");
    });
}

This function takes in various parameters representing user data such as firstName, lastName, phoneNumber, address, birthday, age, idNumber, gender, degree, intake, semester, and course.

These are all String data and in database also these are all defined as Strings

I take the date, nic and phoneNumber as Strings and tried to pass it, but JSON body does not passing these three Strings but it passing other strings. Why is that ??

Dummy data for post

{ "firstName": "John", "lastName": "Doe", "phoneNumber": "1234567890", "address": "123 Main Street", "birthday": "1990-01-01", "age": 31, "idNumber": "123456789012", "gender": "male", "degree": "Bachelor of Science", "intake": "2020", "semester": "Spring", "course": "Computer Science" }

Request Body

{ "regNo": 28, "firstName": "John", "lastName": "Doe", "phoneNo": null, "address": "123 Main Street", "nicNo": null, "gender": "male", "dob": null, "age": "31", "degree": "Bachelor of Science", "intake": "2020", "semester": "Spring", "course": "Computer Science", "enrollments": null }

0

There are 0 answers