0

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!

Stefanvdk
  • 155
  • 1
  • 2
  • 13
  • 1
    I will guess that `getRecipes` is asynchrone. Or the `else { ... }` block is never reached. It seems to me that you left out the most important part of `getRecipes`. – Andreas Louv May 30 '16 at 22:28
  • On which line does it error? – michaelmesser May 30 '16 at 22:28
  • @2426021684 This line `for (i = 0; i < recipes.length; i++) {` – Andreas Louv May 30 '16 at 22:30
  • I would concur with @andlrc, please post the full code for these functions. Also, if it's asynchronous, there may be callbacks involved, which would require changing how the `sendGenericMessage` line is structured. – cyberbit May 30 '16 at 22:32
  • I can see a tell-tale `});` in `getRecipes`. You've been bitten by a callback. Congrats! It happens to every single JS programmer. – Russbear May 30 '16 at 22:50

1 Answers1

0

Your function getRecipes has a callback in it (I can tell by the });). Read up on them here (https://developer.mozilla.org/en-US/docs/Mozilla/js-ctypes/Using_js-ctypes/Declaring_and_Using_Callbacks) or any other source you can find. It usually takes 2 or 3 articles to get a good feel for it.

Basically your recipes object is being populated after some asynchronous task, but the function is return far before then. The way to get the data you need is something like:

function getRecipes(ing, cb) {
  // stuff
  asyncFun(ing, cb)
};

getRecipes(ing, function(err, recipes) {
  buildGenericMessage(recipes);
});
Russbear
  • 1,261
  • 1
  • 11
  • 22
  • Thank you all very much for your quick replies! I got it running and understand now asynchronous js (a bit)! – Stefanvdk Jun 05 '16 at 20:40