search

Home  >  Q&A  >  body text

Error: "friends.find is not a function" when using live server but not localhost

describe: Whenever I switch from using localhost as the API endpoint to the actual backend live URL, I get two errors in my React app. Here are the errors I'm getting:

1

2

3

PATCH https://hobby-hunter-api.onrender.com/users/6488a0ab682e930dcd661111/64628638beaa4cc4aecd3a42 404 (Not Found) in Friend.jsx:25

 

`Uncaught TypeError: friends.find is not a function in Friend.jsx:22

I use MongoDB and Node.js as backend. Appreciate any insights or solutions to resolve this issue. Thanks!

Additional details: If any additional information or code snippets are needed to help diagnose the problem, please let me know.

Friend component code:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

import { PersonAddOutlined, PersonRemoveOutlined } from "@mui/icons-material";

import { Box, IconButton } from "@mui/material";

import { useDispatch, useSelector } from "react-redux";

import { useNavigate } from "react-router-dom";

import { setFriends } from "state";

 

import UserImage from "./UserImage";

 

const Friend = ({ friendId, name, userPicturePath }) => {

  const dispatch = useDispatch();

  const navigate = useNavigate();

  const { _id } = useSelector((state) => state.user);

  const token = useSelector((state) => state.token);

  const friends = useSelector((state) => state.user.friends);

 

  const isFriend = friends && friends.find((friend) => friend._id === friendId); // Check if the friend exists

 

  const patchFriend = async () => {

    const response = await fetch(

      `https://weblinkbackendapi.onrender.com/users/${_id}/${friendId}`,

      {

        method: "PATCH",

        headers: {

          Authorization: `Bearer ${token}`,

          "Content-Type": "application/json",

        },

      }

    );

    const data = await response.json();

    dispatch(setFriends({ friends: data }));

  };

 

  if (!Array.isArray(friends)) {

    return null; // Return null if friends is not an array

  }

 

  return (

    <Box className="text-black flex mb-2 bg-zinc-200 justify-between items-center rounded-lg z-10 ease-in duration-500">

      <Box className="flex justify-between items-center gap-1">

        <UserImage image={userPicturePath} />

        <Box

          onClick={() => {

            navigate(`/profile/${friendId}`);

            navigate(0);

          }}

        >

          <h2 className="text-black font-medium hover:text-white cursor-pointer p-2 mr-1">

            {name}

          </h2>

        </Box>

      </Box>

      <IconButton

        onClick={() => patchFriend()}

        sx={{ p: "0.05rem", fontSize: "0.25rem" }}

      >

        {isFriend ? (

          <PersonRemoveOutlined sx={{ color: "grey" }} />

        ) : (

          <PersonAddOutlined sx={{ color: "grey" }} />

        )}

      </IconButton>

    </Box>

  );

};

 

export default Friend;

P粉489081732P粉489081732549 days ago637

reply all(1)I'll reply

  • P粉928591383

    P粉9285913832023-09-12 09:36:16

    I ended up adding conditional rendering to the current user's friends functionality patch. The problem occurs when the current user requests itself.

    reply
    0
  • Cancelreply