I'm trying to make a function to go through an object and if is there any null values. Here is my code:
async function validateObjectField(course) {
let error = new Error();
const keys = Object.keys(course);
keys.forEach(k => {
const obj = course[k];
if (typeof obj === "object") {
if (Array.isArray(obj)) {
if (Object.size(obj) === 0) {
error.message = `${obj} is required!`;
return error;
}
for (const o of obj) {
error = validateObjectField(o);
if (error) return error;
}
} else {
error = validateObjectField(obj);
if (error) return error;
}
}
if ((typeof obj === "string" && obj.length === 0) || obj === null) {
error.message = `${obj} is required!`;
return error;
}
});
}
I'm using console.log() to observe and see that sometimes the code goes to return statement but the remaining code is still executed. Anyone know why? I got stuck and have no idea what to do now.
In this block of codes:
if (obj.length === 0) {
error.message = `${obj} is required!`;
return error;
}
It did not return the error and break the function.