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.
我正在使用node.js“async”模块,需要使用“map”方法。基本上我有一个包含其他数组的数组。内部数组包含2个元素,一个类型和一个图像文件名。
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.
对于每个内部数组,我想获取由文件名标识的图像的base64编码,并将此编码字符串添加为数组的第三个元素。
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.
我知道arr.push(base64Image)的东西是不正确的东西,但我无法弄清楚如何返回修改后的元素。
In map(arr, iterator, callback) documentation, it is specified:
在map(arr,iterator,callback)文档中,指定了:
"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.
问题是我无法弄清楚如何用新数组提供回调。
3 个解决方案
#1
1
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.
这不起作用,因为getImageEncoding中的回调是使用arr.push的返回值(为1)调用的,而不是arr.push之后的arr,这是你想要的。
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");
});
#2
1
The problem is that you execute your callback with the return value of arr.push
as your result, where you really want an array as the result. Just use concat
instead:
问题是你用arr.push的返回值作为结果执行你的回调,你真的想要一个数组作为结果。只需使用concat:
callback(null, arr.concat(base64Image));
#3
1
You map it properly. You need to use the callback in the iterator, Try the documentation of after
你正确地映射它。你需要在迭代器中使用回调,试试after的文档
var fs = require("fs");
var after = require("after");
function getImageEncoding(tuple, callback){
fs.readFile(arr[1], function(err, data){
if (err) return callback(err)
callback(null, tuple.concat(data.toString('base64'))
});
}
after.map(arr0, getImageEncoding, function(err, results){
console.log("in after.map: " + results + "\n");
});
#1
1
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.
这不起作用,因为getImageEncoding中的回调是使用arr.push的返回值(为1)调用的,而不是arr.push之后的arr,这是你想要的。
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");
});
#2
1
The problem is that you execute your callback with the return value of arr.push
as your result, where you really want an array as the result. Just use concat
instead:
问题是你用arr.push的返回值作为结果执行你的回调,你真的想要一个数组作为结果。只需使用concat:
callback(null, arr.concat(base64Image));
#3
1
You map it properly. You need to use the callback in the iterator, Try the documentation of after
你正确地映射它。你需要在迭代器中使用回调,试试after的文档
var fs = require("fs");
var after = require("after");
function getImageEncoding(tuple, callback){
fs.readFile(arr[1], function(err, data){
if (err) return callback(err)
callback(null, tuple.concat(data.toString('base64'))
});
}
after.map(arr0, getImageEncoding, function(err, results){
console.log("in after.map: " + results + "\n");
});