0

I have a function that fetchs a json data from a server and it must fetch the data if only the condition is met, but it excutes the block even if the condition is false

const getOfferingCourses = async () => {
    if (typeof loggerInfo === "object") {
      if (typeof loggerInfo.section) {
        const formdata = new FormData();
        formdata.append("getOfferingCourse", loggerInfo.section);
        let dep = await fetch(baseUrl + "enroll.php", {
          method: "POST",
          headers: {
            Accept: "application/json",
          },
          body: formdata,
        });
        let depa = await dep.json();
        if (typeof depa !== "undefined") {
          if (depa.status === "success") {
            setOffeing(depa.data.offering);
            setOffCourses(depa.data.courses);
          }
        }
      }
    }
  }

this is the error I got:


Uncaught (in promise) TypeError: Cannot read properties of null (reading 'section')
    at getSchedule (s-schedule.js:43:1)
    at s-schedule.js:61:1
    at commitHookEffectListMount (react-dom.development.js:23150:1)
    at commitPassiveMountOnFiber (react-dom.development.js:24926:1)
    at commitPassiveMountEffects_complete (react-dom.development.js:24891:1)
    at commitPassiveMountEffects_begin (react-dom.development.js:24878:1)
    at commitPassiveMountEffects (react-dom.development.js:24866:1)
    at flushPassiveEffectsImpl (react-dom.development.js:27039:1)
    at flushPassiveEffects (react-dom.development.js:26984:1)
    at react-dom.development.js:26769:1
Ahmed Sbai
  • 10,695
  • 9
  • 19
  • 38
  • condition might be wrong, I don't think `null` is the same type as `undefined` – Chris G Jun 22 '23 at 20:01
  • Which specific line of code produces the error? When you debug and pause on that line of code, what are the observed values being used? – David Jun 22 '23 at 20:06
  • Does this answer your question? [Why is typeof null "object"?](https://stackoverflow.com/questions/18808226/why-is-typeof-null-object) – Heretic Monkey Jun 22 '23 at 23:04

3 Answers3

1

the error occures when you try to check if (typeof loggerInfo.section) but as mentioned loggerInfo is null.
you may wonder how this is possible inside if (typeof loggerInfo === "object") but typeof null is equal to "object" so you need to add another condition in the first if statement:

if(typeof loggerInfo === "object" && loggerInfo !== null)

also update the second if statement this way:

if (typeof loggerInfo.section !== "undefined")

or

if (loggerInfo.section) 
Ahmed Sbai
  • 10,695
  • 9
  • 19
  • 38
1

thanks guys i gave the if condition the loggerinfo it self so it will be falsy in both null or undefined so it won't pass in both cases

if(loggerinfo){
// bluh bluh bluh
}

and it's working

0

References

Differences between null and undefined

why is type of null and object?

Checkout this example we have null and undefined variables here, we check the type of each of them and while undefined is indeed undefined, null is considered an object.

This line if (typeof loggerInfo === "object") { if loggerInfo is null then the condition will be true and it will try to read the next line if (typeof loggerInfo.section) { which causes the error

let depa = null;
let depa2 = undefined;

let nullVar = typeof depa;
let unVar = typeof depa2;

console.log(nullVar)
console.log(unVar)
Chris G
  • 1,598
  • 1
  • 6
  • 18