0

so I'm working on my first HTML-CSS-JS-jQuery project. I have to make a simple tweeter, it's just for learning. One of the features is that the app should render a user's feed with only his tweets, if when at the home feed, you click over a users nametag. I don't really have much experience coding, so It might be a small data type error and I can't notice it, thanks for the help!

This is my renderFeed function:

var renderFeed = function (user) {

  if (user === 'undefined') {
    var index = streams.home.length - 1;
    var tweet = streams.home[index];
  } else {
    var index = streams.users[user].length - 1;
    var tweet = streams.users[user][index];
  }

  while (index >= 0) {
    var $tweet = $('<div class="tweet"></div>');
    $('#feed').append($tweet);

    var formattedDate = jQuery.timeago(tweet.created_at);

    var $profilePhoto = $('<img src="' + tweet.profilePhotoURL + '" class="profile-photo">');
    var $username = $('<span class="username"></span>');
    var $message = $('<p class="message"></p>');
    var $timestamp = $('<span class="timestamp">' + formattedDate + '</span>');
    var $comment = $('<i class="fas fa-comment comment"></i>');
    var $retweet = $('<i class="fas fa-retweet retweet"></i>');
    var $like = $('<i class="fas fa-heart like"></i>');
    var $share = $('<i class="fas fa-share-alt share"></i>');

    $message.text(tweet.message);
    $username.text('@' + tweet.user);

    $($tweet).append($profilePhoto);
    $($tweet).append($username);
    $($tweet).append($message);
    $($tweet).append($timestamp);
    $($tweet).append($comment);
    $($tweet).append($retweet);
    $($tweet).append($share);
    $($tweet).append($like);

    index -= 1;
  }
}

When passed a user, it should then access an array that contains each tweet from that user. It appends a tweet with all its UI components and renders the proper feed.

This is the error I get when running this code:

Cannot read property 'length' of undefined
  • what is in streams and what is in user? – Daniel A. White Jul 10 '21 at 13:09
  • Which line is the error happening on? Either `streams.home` doesn't exist or `streams.users[user]` doesn't exist. – Barmar Jul 10 '21 at 13:10
  • In the code provided, `.length` only appears twice, so the error is telling you that, at the time it runs, the value of `streams.home` or `streams.users[user]` is undefined. This means `streams` or `streams.users` *is* defined, but the `home` or `[users]` part is not generating a result - eg `user` is not a valid value for the index – freedomn-m Jul 10 '21 at 13:12
  • @DanielA.White streams is an object with 2 main properties. The first is called home and contains all the tweets, no matter the user. The second property is users, which contains an object, where each key is a user and the value is an array with all the tweets from that particular user. – Sebastian Carazo Jul 10 '21 at 13:13
  • 1
    The error occurs on the first statement of the ```else``` statement. where I declare index and tweet when user is not passed as an argument @Barmar – Sebastian Carazo Jul 10 '21 at 13:15
  • 1
    So `user == undefined`, not `'undefined'` See [How can I check for “undefined” in JavaScript?](https://stackoverflow.com/questions/3390396/how-can-i-check-for-undefined-in-javascript) which leads to: [Detecting undefined](https://stackoverflow.com/a/416327/2181514) – freedomn-m Jul 10 '21 at 13:17
  • @freedomn-m it works, but now Im just rendering the same tweet everytime... hmm ***EDIT:*** I declared tweet inside the while loop with a conditional checking it it has a user as an argument – Sebastian Carazo Jul 10 '21 at 13:21
  • 1
    Does this answer your question? [Detecting an undefined object property](https://stackoverflow.com/questions/27509/detecting-an-undefined-object-property) – freedomn-m Jul 10 '21 at 13:24
  • I think it was more a matter of checking if it was passed at all, not checking that the value of it was undefined. @freedomn-m – Sebastian Carazo Jul 10 '21 at 13:26
  • @SebastianCarazo yes, that's which checking `undefined` does - check if it's been passed as a parameter. – freedomn-m Jul 10 '21 at 13:52

0 Answers0