0

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

    }
4
  • 7
    Where is the problem? Are you getting any errors? Commented Dec 16, 2013 at 19:49
  • Why did I get a -1 lol? Basically the (logic) error im getting is that the assignment usersitems[i].photos = itemimages; is not assigning Commented Dec 17, 2013 at 0:09
  • You got a -1 because the initial post of your question was entirely unclear. What do you mean "not assigning"? Commented Dec 17, 2013 at 15:23
  • Aha. Essentially the console of usersitems after it has been called back callback(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. Commented Dec 17, 2013 at 17:23

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.