xtr33me
xtr33me

Reputation: 1106

Node.js Mongoose keeps adding same single element instead of all of them

In my code below, my value printed out at the console.log is correct, but when I search and go about entering the objects into the db, all the objects in the db contain the same hex, and image path but the id's are different. I tried first using findOne but the resulted in the same outcome. I am new to MongoDb so I am assuming it is just somethign I am doing stupid. Any ideas please send them my way :)

exports.addImage = function(req,res){
var params = req.body;
var colors = params.color;
var passedImg = params.path;
var ndxobj;
for(var index in colors){
    ndxobj = colors[index];
    //Values here are the correct index and contain valid data
    console.log("col: ", ndxobj);


    var query = clrModel.find({hex: ndxobj.hex}, function(err,result){
        if(!err && result.length > 0){
            console.log(result);
        }else if(!err){
            //We have an empty db for the searched obj
            var locclr = new clrModel({
              hex:      ndxobj.hex      
            });

            locclr.img.push({path:passedImg, date:ndxobj.imagedate});
            locclr.save(function(error, data){
                if(error){
                    console.log("Error in addImage find call: ",error);
                    res.json(error);
                }
                else{
                    console.log("Saving: ",data);
                    res.json(data);
                }
            });
        }else {
            //Handle error
        }
    });
}

};

Upvotes: 0

Views: 58

Answers (1)

andyg0808
andyg0808

Reputation: 1403

I think that your paths are all the same because you set path to be passedImage, and passedImage is not updated from each index, but is set at the top of your code sample. As for the hex values being all the same, that seems to be happening because the callbacks are closing over ndxobj, so by the time they're called, all of them are looking at the same value. To make that work, you'll want to use a function to create your callbacks, something like what follows (hopefully I closed all my parens & brackets...). See this StackOverflow post for more info.

exports.addImage = function(req,res){
  var makeCallback=function(ndxobj){
    return function(err,result){
      if(!err && result.length > 0){
          console.log(result);
      }else if(!err){
          //We have an empty db for the searched obj
        var locclr = new clrModel({
          hex:      ndxobj.hex      
        });

        locclr.img.push({path:passedImg, date:ndxobj.imagedate});
        locclr.save(function(error, data){
          if(error){
            console.log("Error in addImage find call: ",error);
            res.json(error);
          }else{
            console.log("Saving: ",data);
            res.json(data);
          }
        });
      }else{
        //Handle error
      }
    };
  });
  var params = req.body;
  var colors = params.color;
  var passedImg = params.path;
  var ndxobj;
  for(var index in colors){
    ndxobj = colors[index];
    //Values here are the correct index and contain valid data
    console.log("col: ", ndxobj);


    var query = clrModel.find({hex: ndxobj.hex}, makeCallback(ndxobj.hex));
  }
};

Upvotes: 1

Related Questions