I am working in node.js and for some reason cannot assign to an array of objects. I know I've had this before but can't figure it out. I think it must be something to do with either the locality of variables. The function takes 3 arrays. The first (itemphotolength) is an array that contains the number of photos for each item. The second (fbitemphotos) contains an serial sequential list of photos for all items. The third (useritems) is an array of objects containing the items. Thus the function goes the fbitemphotos array adding the number of photos defined in itemphotolength to the corresponding element in useritems (the actual item). The only problem is the most important assignment usersitems[i].photos = itemimages; is not working?!?!?! Please help me!?!?!?
Here's the code. Would be nice to know the solution for the future.
exports.makeitems = function (itemphotolength, fbitemphotos, usersitems, callback) {
function convert( i, next)
{
var itemimages = new Array();
var x = 0;
while(x < itemphotolength[i])
{
itemimages.push(fbitemphotos.photos[x]);
x++;
console.log('testing x');
console.log(x);
}
console.log('testing itemimages');
console.log(itemimages);
usersitems[i].photos = itemimages;
console.log('testing useritems');
console.log(usersitems);
next();
}
function iterator(i)
{
if(i < usersitems.length) {
convert(i, function(err){
if(err) {
console.log(err);
} else {
iterator(i + 1);
}
});
} else {
callback(usersitems);
}
}
iterator(0);
}
usersitems[i].photos = itemimages;is not assigningcallback(usersitems);bears no effect of the assignment mentioned. This is even true of the console of the usersitems directly after the assignment in each call to the function convert.