Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

126
Views
Safest way to guard against missing and undefined parameters?

I have a function that is passed an argument from a network call and I want to guard against these arguments when they are null, missing, or otherwise undefined. Can I simply default each property to null and then check against them being null as my guard? Or is there a safer way?

exports.pushNotify = functions.https.onCall((data, _context) => {
    const recipientUserId = data.recipientUserId || null;
    const senderUserId = data.senderUserId || null;
    const senderName = data.senderName || null;
    const messageText = data.messageText || null;

    if (recipientUserId != null && senderUserId != null && senderName != null && messageText != null) {
        // proceed
    } else {
        // terminate
    }
});
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

If truthiness is what you're checking against for your arguments, you can clean this code up with destructuring and truthy evaluation. That would look like this:

    exports.pushNotify = functions.https.onCall((data, _context) => {
        const { recipientUserId, senderUserId, senderName, messageText } = data;
        const valid = recipientUserId && senderUserId && senderName && messageText;
        if (!valid) {
            // terminate
        }

        // proceed
    });

A simple way to think about a truthy value is that it is not null, undefined, empty string, or 0. See a table of all truthy/falsy values here: https://dorey.github.io/JavaScript-Equality-Table/.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!