Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7571361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:39:26+00:00 2026-05-30T15:39:26+00:00

I am using node.js async module and need to use the map method. Basically

  • 0

I am using node.js “async” module and need to use the “map” method.
Basically I have an array that contains other arrays. The inner arrays contains 2 elements, a type and an image filename.

var arr0 = [];
var arr1 = ["type1", "image1.jpg"];
jsonArr.push(obj1);
var arr2 = ["type2", "image2.jpg"];
jsonArr.push(obj2);

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.

I’m doing something like this:

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");
});

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.

In map(arr, iterator, callback) documentation, it is specified:

“The iterator is called with an item from the array and a callback for when it has finished processing.”

The thing is I cannot figure out how to feed the callback with the new arrays.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T15:39:27+00:00Added an answer on May 30, 2026 at 3:39 pm

    This doesn’t work because callback in getImageEncoding is called with the return value of arr.push (which is 1), not arr after arr.push, which is what you want.

    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
            arr.push(base64Image);
            callback(err, arr);
        });
    }
    
    async.map(arr0, getImageEncoding, function(err, results){
    console.log("in async.map: " + results + "\n");
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following XPATH line: //det[@nItem=1]/prod/cProd That successfully selects the desired node using
I'm using async module (see https://github.com/caolan/async ) for Node.js and my question is... Why
Scenario I have a Node.JS service (written using ExpressJS ) that accepts image uploads
I have simple JavaScript code that is using the Ajax API for fetching a
I am using the async library ( https://github.com/caolan/async ) on node trying to execute
I'm using node.js v0.6.10 although I've got the same issue on 0.6.7 . Basically
I'm using Node.js+Express, and I've come across a case where I need to send
I am using node-mysql module ( https://github.com/felixge/node-mysql ) OR ( http://utahjs.com/2010/09/22/nodejs-and-mysql-introduction/ ) . Is
Any ideas on an async directory search using fs.readdir? I realize that we could
I have problems during serialization/deserialization. I'm using a WCF service (that used .NET framework)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.