map through array nodejs

I have a stripe function where I am trying to push the images into the array for each product. This is part of the prebuilt stripe checkout page… here’s my function at whole:

//defining arrays
var productsArray = [];
var priceArray = [];
var imageArray = [];
//query to database

var productsStripe = "select * from " + tableID + "";
ibmdb.open(db2ConnString, function(err, conn) {
    if (err) return console.log(err);
  conn.query(productsStripe,  async function (err, rows) {
    if (err) {
      console.log(err)
    }
    console.log(rows)

    var itemName = ""
    var itemPrice = ""
    var totalNewPriceTest = ""

    for(var i = 0; i < rows.length; i++)     
    //inserting items and prices into arrays
     productsArray.push(rows[i]['NAME'])
     priceArray.push(rows[i]['PRICE'])
     imageArray.push(rows[i]['IMAGE_URL'])

    }
   
  
    //stripe
            // loop over products array to construct the line_items
        const items = productsArray.map((product, i) => {
            return {
            price_data: {
                currency: 'CAD',
                product_data: {
                name: product,
                images: imageArray[i]
                },
                unit_amount: parseInt(priceArray[i], 10) * 100,
            },
            quantity: 1,
            };
        });

its returning the error: (node:39728) UnhandledPromiseRejectionWarning: Error: Invalid array

how do I get the image array to populate the stripe page with images too? I pushed the values into the array, and am calling them , but not sure why it is returning invalid array. any ideas?

Answer

Observations
  • imageArray is array containing image urls for different products, one for each product in the productArray.
  • From the name of the prop images, the error message and the error line info provided by the OP, the prop images ought to be an array.

In the code, image: imageArray[i] means that the image prop is assigned a string but it should be array. So changing it with [imageArray[i]], we are assigning an array of length one to image prop containing image url for that product.

Leave a Reply

Your email address will not be published. Required fields are marked *