Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
mndozkn opened this issue Apr 8, 2023 · 3 comments
Open

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

mndozkn opened this issue Apr 8, 2023 · 3 comments

Comments

@mndozkn
Copy link

mndozkn commented Apr 8, 2023

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
Copy link
Author

mndozkn commented Apr 17, 2023

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
Copy link

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
Copy link
Author

mndozkn commented Apr 20, 2023

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 :))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants