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?