What is the proper way to use only variables that contain data in Node JS?

Greg

I have been researching this in Node JS and Swift. I have a "form" in my iOS app that gives me the following 3 options:

  • update the expiration date of credit card
  • update the expiration year of credit card
  • update the default credit card entirely

There may be instances where the user is only updating the default card, only updating one of the dates or both and not the default card. both dates.etc, any possibly scenario for updating.

My question is, how can I allow this function to accept blank or null input? I know I can set the CONST variable as null to begin with, but how to I prevent that from being submitted to stripe as blank and either wiping out the data or error out? For example, if const newdefault is blank, how can I make sure that default_source is not updated with blank data?

Would I use the filter command and put these into an array? These are already in JSON strings, so I am not sure an array would work. Additionally, if all 3 are blank (meaning, no one is updating the expire month or year, or selecting default, then they will not be able to hit the function, I have a null checker in my Swift code)

See code below with comments::

exports.updateCard = functions.https.onCall((data, context) => {
    const stripeId = data.stripeId;  // needed as users stripe ID
    const cardId = data.cardId;      // the current card they are updating, comes from the app
    const month = data.expmonth;    // this may or may not be blank
    const year = data.expyear;      // this may or may not be blank
    const newdefault = data.newdefault;  //this may or may not be blank

    return stripe.customers.updateSource(stripeId, cardId,
        {
            exp_month: month,  <--- how can I make sure this is not updated if month is blank?
            exp_year: year  <--- how can I make sure this is not updated if year is blank?
        }
    ).then(() => {
        return stripe.customers.update(stripeId,
            {
                default_source: newdefault <--- how can I make sure this is not updated if newdefault is blank?
            }
        ).then(() => {
            console.log(card);
            return { myReturn: card };
        }).catch((err) => {
            console.log(err);
        });
    });
});

In my research I have found that there is a command called array.filter that can be used. However, these are not arrays, but JSON strings. Here is the example I found on StackOverflow along with the link.
StackOverflow article on .filter

var array = [0, 1, null, 2, "", 3, undefined, 3,,,,,, 4,, 4,, 5,, 6,,,,];

var filtered = array.filter(function (el) {
  return el != null;
});

console.log(filtered);

Can someone please advise if I can use this method, if not, provide an example of a method I can use to achieve only allowing submission to stripe if the variables have data. Possibly a switch statement?

Doug Stevenson

It will probably be more straightforward if you just populate an object with a property based on the explicit condition that you want:

const obj = {}
if (thing) {
    obj.thing = thing
}
if (otherthing) {
    obj.otherthing = otherthing
}

stripe.customers.updateSource(obj)

In other words, there's no need to specify the entire object at once when you call any API. Just build up the object based on the conditions you want.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

What is the proper way to use the node.js postgresql module?

Node.js: What is the proper way of splitting code?

In Node.js 7 what is the proper way to suppress UnhandledPromiseRejectionWarning?

What's the proper way to require in Node.js?

What is the proper way to set up Node.js and MySQL?

Proper way to use .populate() mongodb Node.js

Proper Way to Read a Data File in Javascript/Node.js?

What is the proper way to use Nat/Natural in a singletons data type?

What are the proper use cases for process.nextTick in Node.js?

What is the proper way to use inotify?

What is the proper way to use continue?

What is the proper way to use if _ is _ or .isKind(of: )

What is the proper way to use IF THEN in AQL?

What is the proper way to bundle variables in python

What is the proper way to pass multiple variables to MIMEText

What is the proper way to retrieve all class definitions that contain a specific mixin?

What's the proper way to use variables defined in the main thread in a child thread in Rust?

What is the proper way for defining javascript variables in different cshtml files which have a common js file?

Proper way of sending an html response in Node Js

What is the proper way to use @property in python

what is the "proper" way to use django REST framework?

What is the proper way to use a .equals method in Java?

What is the proper way to use an alternative binary

What is the proper way to use multiple layouts in ReactJS

What is the proper way to use bit array in Rust?

What is the proper way to use Toolbar and SwipeRefreshLayout?

What is the proper way to use React Memo with Flow?

what is the proper way to use $nin operator with mongoDB

What is the proper/right way to use Async Storage?