Cannot read property 'length' of undefined, i dont seem to know whats going wrong

‘use strict’;

// import all required modules
const accounts = require (’./accounts.js’);
const logger = require(’…/utils/logger’);
const recipieBook = require(’…/models/Foodie!.js’);
const comment = require(’…/models/post_comment.js’);

// create start object
const start = {

// index method - responsible for creating and rendering the view
index(request, response) {

const loggedInUser = accounts.getCurrentUser(request);'start rendering');


  const recipies = recipieBook.getAllRecipies();
   let numRecipies = recipies.length;
   let numIngredients = 0;

  for (let i in recipies) {
    numIngredients = numIngredients + recipies[i].ingredients.length;


  const viewData = {
    title: 'Welcome Foodie!',
   totalRecipies: numRecipies,
  totalIngredients: numIngredients ,
    fullname: loggedInUser.firstName + ' ' + loggedInUser.lastName,

  response.render('start', viewData);
  else response.redirect('/');



// export the start module
module.exports = start;

This error probably means that, in your for loop, at some point (for some value of i) recipes[i].ingredients does not exist. Therefore, your code cannot get the length of that undefined thing. Try using console.log to log out recipes[i].ingredients in the for loop, and seeing whether it’s always what you expect it to be.

1 Like

The thing is it worked fine before ,but whenever i added extra code to it it stopped working forcing me to constantly rewind it also works whenever i send it to someone else to open

But yea ill try that thanks