Note that there are some explanatory texts on larger screens.

plurals
  1. POCannot use "map" function within async module
    text
    copied!<p>I am using node.js "async" module and need to use the "map" method.<br> Basically I have an array that contains other arrays. The inner arrays contains 2 elements, a type and an image filename. </p> <pre><code>var arr0 = []; var arr1 = ["type1", "image1.jpg"]; jsonArr.push(obj1); var arr2 = ["type2", "image2.jpg"]; jsonArr.push(obj2); </code></pre> <p>For each inner array, I want to get the base64 encoding of the image identified by the filename and add this encoding string as the third element of the array.</p> <p>I'm doing something like this: </p> <pre><code>var fs = require("fs"); var async = require("async"); function getImageEncoding(arr, callback){ console.log("getEncoding:" + arr + "\n"); // Get image filename image = arr[1]; // Read file and get base64 encoding fs.readFile(image, function(err, original_data){ var base64Image = original_data.toString('base64'); console.log("test:" + base64Image + "\n"); // Modify current arr by appendingthe base64 encoding of the image callback(null, arr.push(base64Image)); }); } async.map(arr0, getImageEncoding, function(err, results){ console.log("in async.map: " + results + "\n"); }); </code></pre> <p>I know the arr.push(base64Image) stuff is the thing that is not correct, but I cannot figure out how to return the modified element. </p> <p>In map(arr, iterator, callback) documentation, it is specified: </p> <p>"The iterator is called with an item from the array and a callback for when it has finished processing." </p> <p>The thing is I cannot figure out how to feed the callback with the new arrays.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload