My use-case: using each SKU in my SQL database, I will compare its respective price with the current, realtime price retrieved using API. Later on, I will compare these prices and overwrite the price in my DB with the realtime price.
Query
This is from another Retool post about looping through arrays. My goal: retrieve the first SKU from the table and store it in dynamicSKU, pass it into getProducts (see below), and store the price retrieved from getProducts as a new variable (see comparePrices below). I will then compare the price from the SKU in the table with the price from getProducts. If there's a change (+/-) from the price in my database, I will overwrite the price in my database (I haven't made it this far yet - I'm still trying to get to the point of comparing prices).
This query hasn't really been working. In fact, it crashes my app every time I run it. Everything else seems to be working so far. Question #1: how can I amend this loop to meet the above need?
const bridalArray = await getAllBridalDB.trigger({});
//console.log(bridalArray)
const bridalArraySKUs = bridalArray.Sku
const query = getProducts
const promises = bridalArraySKUs.map((Sku) => {
return query.trigger({
additionalScope: {
dynamicSKU: Sku
}
});
});
return Promise.all(promises);
getAllBridalDB
Simply imports SQL database into table. Each row has a unique SKU (not pictured)
Example of SQL data in table:
getProducts
comparePrices
I'm working with an array of nested objects. This script logs the price as a decimal. It should work for every SKU as the JSON structure doesn't change.
If I simply type in a SKU in the value of getProducts, compareProducts logs the price successfully. I will need this to be dynamic in production (hence {{dynamicSKU}}).
Question #2: how can I dynamically store this decimal to later compare with my DB price? I'd probably run the comparison in real-time in this script.
//Passes thru every object thru price
let values = Object.values(getProducts.data.Products[0].Price)
//use values[0] because values [1] is the currency denomination as a string (which is not needed)
let currentPrice = values[0] //current price of given SKU as a decimal
console.log(currentPrice) //result: 1610.93
TL;DR:
- Query: run loop to pass each SKU from getAllBridalDB as {{dynamicSKU}} in getProducts API call.
- comparePrices: assign price from JSON retrieved in {{dynamicSKU}} API call as a dynamic variable
- (Later use case): compare price from getAllBridalDB to price from comparePrices.
3a. Update DB price as needed.