I'm new with working with JSON (and coding in general) and I'm stuck for a couple of hours on what seems to me like a simple problem. I'm trying to pass an array from one method to the next but get the following errormessage: TypeError: Cannot read property 'length' of undefined.
Via the logs I can see that the variable is set when return by methodA but is undefined when used in methodB.
Here are my code samples:
function getRecipes(ingredients) {
....
} else {
var output = [];
recipes = JSON.parse(body);
recipelist = recipes.recipes;
//console.log("Recipes: " + recipes);
//console.log("Recipeslist: " + recipelist);
for (i=0; i<recipelist.length; i++) {
test = recipelist[i];
//console.log(test);
output.push(test);
}
console.log("output");
console.log(output);
return output;
}
});
}
function buildGenericMessage(recipes) {
var elements = [];
console.log("Recipes");
console.log(recipes);
for (i = 0; i < recipes.length; i++) {
//....
}
The code is executed by:
sendGenericMessage(sender, buildGenericMessage(getRecipes(getIngredients(text))));
This is the console log of the variable output:
[ { publisher: 'My Baking Addiction',
f2f_url: 'http://food2fork.com/view/035865',
title: 'The Best Chocolate Cake',
source_url: 'http://www.mybakingaddiction.com/the-best-chocolate-cake-recipe/',
recipe_id: '035865',
image_url: 'http://static.food2fork.com/BlackMagicCakeSlice1of18c68.jpg',
social_rank: 100,
publisher_url: 'http://www.mybakingaddiction.com' },
{ publisher: 'My Baking Addiction',
f2f_url: 'http://food2fork.com/view/e7fdb2',
...},
..]
And this is the console log for the recipes variable:
Recipes:
undefined
Help would be very much appreciated!