简体   繁体   中英

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

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM