Mantissa
Mantissa

Reputation: 91

Can't assign to an array of objects javascript

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

    }

Upvotes: 0

Views: 2250

Answers (3)

Theodor349
Theodor349

Reputation: 173

If it is a Mongoose problem you can use the .toObject({getters: true, virtuals: true}) on the object you get from the database.

More information is available here: alexanderzeitler.com and Mongoose Documentation.

Hope this helps.

Upvotes: 1

nidal
nidal

Reputation: 470

I assume your object usersitems is an object coming from a mongoose query. Objects outputted in mongoose cannot be altered directly and do not act the same as normal Javascript object. So a quick fix for now is to change the object into a standard Javascript object like so.

exports.makeitems = function  (itemphotolength, fbitemphotos, usersitems, callback) {
    var useritemscopy = JSON.parse(JSON.stringify(usersitems));
    function convert(i, next) {

        var itemimages = new Array();
        var x = 0;
        while(x < itemphotolength[i]) {       
            itemimages.push(fbitemphotos.photos[x]);
            x++;
        }
            useritemscopy[i].photos = itemimages;
            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);
}

Upvotes: 0

Alex Netkachov
Alex Netkachov

Reputation: 13522

Your code works quite well when it is called as follows:

var itemphotolength = [1],
    fbitemphotos = { photos : ['qqq'] },
    usersitems = [ { photos : [ ] } ];
makeitems(itemphotolength, fbitemphotos, usersitems,
    function (a) { console.log('a:', a); })

Upvotes: 0

Related Questions