I have a "character" model with two columns which save JSON objects, which store arrays of "limb" model objects (as JSON objects). On a page the user selects which "limbs" belong in either of the two columns ("equipped" or "spare"), and that selection is sent to the server via a form.
I know that the form works, because the next page successfully displays all six of the "limb" objects which exist in JSON arrays. The exact same lists should get saved into the "character's" "spare" and "equipped" columns.
90% of the time it works perfectly. But occasionally it will happen that only four or five of the "limb" objects actually get stored in the "character" database, even though the exact same lists successfully displayed all six of the "limbs."
The process is that the form will send an array of ID numbers, and then the server will instantiate "limb" objects from the database based on those IDs. As I said, this part works flawlessly 100% of the time, because the page always shows all six "limb" objects. But occasionally the exact same lists will be missing one or two entries when stored in the "character's" columns in the database.
Here is the relevant code:
updateCharacter: function(req, res) {
if(req.session.user){
// FIRST get the form info and store it:
var equipped_limb_ids = req.param('char_equipped_limbs').split(",");
var spare_limb_ids = req.param('char_spare_limbs').split(",");
var new_name = req.param('new_char_name');
var char_id = req.param('char_id_out');
// Next get fresh limbs based on the limbs IDs:
var spare_limbs = { "limbs": [] };
var equipped_limbs = { "limbs": [] };
for(var i=0; i<spare_limb_ids.length; i++){
var limb_id = spare_limb_ids[i].replace(/\W/g, '');
Limb.findOne().where({ 'id': limb_id })
.exec(function(err, result){
if(result){
spare_limbs["limbs"].push(result);
}
});
}
for(var i=0; i<equipped_limb_ids.length; i++){
var limb_id = equipped_limb_ids[i].replace(/\W/g, '');
Limb.findOne().where({ 'id': limb_id })
.exec(function(err, result){
if(result){
equipped_limbs["limbs"].push(result);
}
});
}
// Prepare info to update the character, and send the same info to the front page
var updated_info = {
name: new_name,
equipped_limbs: equipped_limbs,
spare_limbs: spare_limbs
};
Character.update( { id:char_id }, updated_info )
.exec( function(err, char) {
return res.view('viewcharacter', {
name:new_name,
spares: spare_limbs,
equipped: equipped_limbs,
char_id: char_id
});
});
}else{
return res.view('login');
}
}
So you can see that the exact same list is being stored in the database which is also being sent to the 'viewcharacter' page, but 'viewcharacter' ALWAYS shows all of the "limbs" which were sent and created, but not all the created "limbs" are stored in the database when I update the "character" object.
Thanks in advance