The right handling of missing data

Let’s say i do a query on backend, sending data from frontend to backend as → OBJECT…

let options = {
    database:       mainMenuDB,
    limit:          100,
    skip:           0
}

My code on backend, get all the informations and works well.

But what if i want be able to exclude one or multiple VALUES and do not include them into the → OBJECT → like…

let options = {
    database:       mainMenuDB
}

My code breaks down, because on backend still all 3 values are expected, but 2 values are completely missing.

  • an if-else-statement do not help
  • also → try + catch, seems not to be the wrong solution.

Is there a possible solution for such a situation?

BTW, this is …

let options = {
    database:       mainMenuDB,
    limit:          undefined, <---- ...not what i am searching for
    skip:           undefined <---- ...not what i am searching for
}

Why not just check to see if the property is defined in the object, and if not supply a default.

@yisrael-wix

You mean …
if(options.limit){query.limit(x)}
if (options.skip){query.skip(y)}
???

Ok, i think now i got you, after reading your suggestion once again…


let options = {
    database:       mainMenuDB,
    limit:          100,
    skip:           0
}

let resLimit = 'limit' in options; 
console.log(resLimit); // true/false

resSkip = 'skip' in options;
console.log(resSkip); // true/false

EDIT: → TESTED → Works perfect → like expected!

Thanks Yisrael !!!