Monday, 12 June 2017

How to access results of shopify-node-api request outside callback function

I'm building a Shopify app and I'm running into an issue while using the shopify-node-api module. This is the code I'm working with:

collectProducts: ['storedProducts', function(results, callback) {

  const collected_products = results.storedProducts;

  for (var i = 0; i < collected_products.length; i++) {

    Shopify.post('/admin/collects.json', {
      "collect": {
        "product_id": collected_products[i].product_id,
        "collection_id": process.env.DAILY_COLLECTION
      }
    }, function(err, data, headers){
      collected_products[i].collect_id = data.collect.id;
    });
  }

  callback(null, collected_products);
}],

For clarity's sake, the collectProducts item is part of an async function. I'm trying to gather the collect ID from the response to the post request and update the collect_id value in collected_products. The issue is I can't seem to access the collected_products array from inside the callback function for the post request. Is there a way to 1. simply return that value for each iteration of the for loop or 2. access the collected_products array from within that callback function to store those values?

Thanks in advance for any answers!



via paperbeatsscissors

No comments:

Post a Comment