ed-roh / mern-social-media

Complete React MERN Full Stack Social Media App
992 stars 859 forks source link

Cannot read properties of null (reading 'friends') #52

Open mndozkn opened 1 year ago

mndozkn commented 1 year ago

i got this problem when i trying to add friend someone. but i don't get that when i add friend myself. how can i fix that ?

mndozkn commented 1 year ago

Friend.jsx :

const isFriend = friends.length && friends.length > 0 ? friends.find((friend) => friend._id === friendId) : false;

  const patchFriend = async () => {
    const response = await fetch(
      `http://localhost:3001/users/${_id}/${friendId}`,
      {
        method: "PATCH",
        headers: {
          Authorization: `Bearer ${token}`,
          "Content-Type": "application/json",
        },
      }
    );
    const data = await response.json();
    dispatch(setFriends({ friends: data }));
  };

server/controllers/users.js :

export const getUserFriends = async (req, res) => {
  try {
    const { id } = req.params;
    const user = await User.findById(id);

    const friends = await Promise.all(
      user.friends.map((id) => User.findById(id))
    );
    const formattedFriends = friends.map(
      ({ _id, firstName, lastName, occupation, location, picturePath }) => {
        return { _id, firstName, lastName, occupation, location, picturePath };
      }
    );
    res.status(200).json(formattedFriends);
  } catch (err) {
    res.status(404).json({ message: err.message });
  }
};

/* UPDATE */
export const addRemoveFriend = async (req, res) => {
  try {
    const { id, friendId } = req.params;
    const user = await User.findById(id);
    const friend = await User.findById(friendId);

    if (user.friends.includes(friendId)) {
      user.friends = user.friends.filter((id) => id === friendId);
      friend.friends = friend.friends.filter((id) => id === id);
    } else {
      user.friends.push(friendId);
      friend.friends.push(id);
    }
    await user.save();
    await friend.save();

    const friends = await Promise.all(
      user.friends.map((id) => User.findById(id))
    );
    const formattedFriends = friends.map(
      ({ _id, firstName, lastName, occupation, location, picturePath }) => {
        return { _id, firstName, lastName, occupation, location, picturePath };
      }
    );

    res.status(200).json(formattedFriends);
  } catch (err) {
    res.status(404).json({ message: err.message });
  }
};

i think the problem is array(friends) is empty and function cant find something in array(friends)

whiteforestcat commented 1 year ago

Hi,

I faced the same issue too. To resolve it, you need to drop your entire collection and run the seeding data again in your index.js file in your server (remember to comment it out back after running once)

// seeding data upon server launch
// run only once
// User.insertMany(users)
// Post.insertMany(posts)

Then try adding friends again

Hope it works! :)))

mndozkn commented 1 year ago

Hi,

I faced the same issue too. To resolve it, you need to drop your entire collection and run the seeding data again in your index.js file in your server (remember to comment it out back after running once)

// seeding data upon server launch
// run only once
// User.insertMany(users)
// Post.insertMany(posts)

Then try adding friends again

Hope it works! :)))

thanks for your help. I solved the issue on my first try. good programming to you :))