Updating JSON Array Specific String
The issue I am having is that I am returning a set of users credit cards from stripe. Because Stripe separates the default card or "source" from the cards object, I need to append the respond back to my app. my app has a struct with an isDefault BOOL variable. The app part can't really be changed.
In the JSON code I posted below, I have made some comments with aarows as to where I want the new key: value to go. In a nutshell, if any of the cards in their own {} matches the default card the user has, then I want to insert the key value: "isDefault": true ONLY into the {} that matches the default card.
I have been toying with this for a while and I am stuck. I have used a for loop to search the array for the matching card ID, but can't figure out how to append in the specific {} of the card, its just going into the whole array.
JSON Response with comments:
{
"cardsData": [
{
"id": "card_328746328746",
"object": "card",
"address_city": null,
"address_country": null,
"address_line1": null,
"address_line1_check": null,
"address_line2": null,
"address_state": null,
"address_zip": null,
"address_zip_check": null,
"brand": "Discover",
"country": "US",
"customer": "cus_djklfhdskljfhs",
"cvc_check": "pass",
"dynamic_last4": null,
"exp_month": 9,
"exp_year": 2023,
"fingerprint": "dfhsdkljfsdlkf",
"funding": "credit",
"last4": "1117",
"metadata": {},
"name": null,
"tokenization_method": null
},
{
"id": "card_kjdslhfjdhf", <-- HAPPENS TO BE DEFAULT CARD, NEED TO INSERT HERE
"object": "card",
"address_city": null,
"address_country": null,
"address_line1": null,
"address_line1_check": null,
"address_line2": null,
"address_state": null,
"address_zip": null,
"address_zip_check": null,
"brand": "Visa",
"country": "US",
"customer": "cus_dkljfhdkjfhd",
"cvc_check": "pass",
"dynamic_last4": null,
"exp_month": 4,
"exp_year": 2054,
"fingerprint": "dfjsklkjfhsd",
"funding": "credit",
"last4": "4242",
"metadata": {},
"name": null,
"tokenization_method": null
"isDefault": true <-- THIS CURRENTLY DOES NOT EXIST, THIS IS WHERE I NEED HELP TO GET THIS INSERTED
}
]
}
Node JS/Cloud Function Code:
exports.getCardsTest = functions.https.onRequest((req, res) => {
if (req.method !== 'POST') {
return;
}
const customerId = req.body.stripeId;
stripe.customers.retrieve(
customerId,
{
},
function (err, customer) {
const cardsData = customer.sources.data;
const defaultCard = customer.default_source;
var isDefault = false; //check of vairable value
console.log('isDefault before:', isDefault);
for (var i = 0; i < cardsData.lenth; i++); {
if (cardsData[i].id == defaultCard);
isDefault = true;
cardsData.push({ isDefault }); <--THIS IS WHERE I NEED HELP, I NEED isDefault: true APPENDED TO THE ARRAY STRING {} MATCHING THE KEY VALUE OF THE DEFAULT CARD
};
console.log('isDefaultAfter:', isDefault); // check to make sure IF statement is working
return res.status(200).send({ cardsData });
}
);
});
Answer
you can update the value in the array like this
for (var i = 0; i < cardsData.length; i++); {
if (cardsData[i].id === defaultCard) {
cardsData[i].isDefault = true
}
}
your if
was useless since a part was missing, also you were pushing inside the array of cards, I think that
Related Questions
- → Maximum call stack exceeded when instantiating class inside of a module
- → Browserify api: how to pass advanced option to script
- → Node.js Passing object from server.js to external modules?
- → gulp-rename makes copies, but does not replace
- → requiring RX.js in node.js
- → Remove an ObjectId from an array of objectId
- → Can not connect to Redis
- → React: How to publish page on server using React-starter-kit
- → Express - better pattern for passing data between middleware functions
- → Can't get plotly + node.js to stream data coming through POST requests
- → IsGenerator implementation
- → Async/Await not waiting
- → (Socket.io on nodejs) Updating div with mysql data stops without showing error