Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Throw an error if undefined?

Is there a way to throw an error if property could not be found from data.

Problem is, it is mapping undefined instead of throwing error.

const insertIntoTable = function(data) {
    return new Promise((resolve, reject) => {
        const entry = {
            Id: data.id,
            Method: data.Method,
            Status: data.PaymentStatus,
            InsertedAt: (new Date().getTime())
        }
    }).catch((error) => {
       console.log(error); 
    });
}
like image 744
user88432 Avatar asked Aug 14 '26 18:08

user88432


1 Answers

You can check if the properties are undefined by comparing it to undefined. For example, if you wanted to check the id property, you could use

if(data.id === undefined){
     throw new Error();
}
like image 98
Jessica Bunyan Avatar answered Aug 16 '26 11:08

Jessica Bunyan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!