Skip to content
TheChatArchiveThe Chat Archive
gpt-4-0314· openaiHugging Face Suzhen/CodeChat

i'm trying to fetch comments but i got error comments.map is not a function [import…

  1. User

    i'm trying to fetch comments but i got error comments.map is not a function [import React, { useEffect, useState } from "react"; import { useParams } from "react-router-dom"; import axios from "axios"; import "../style.scss";

    function View() { const [video, setVideo] = useState(null); const [commentText, setCommentText] = useState(""); const [comments, setComments] = useState([]);

    const [videoUrl, setVideoUrl] = useState(null); const { id } = useParams();

    useEffect(() => { const fetchVideo = async () => { try { const response = await axios.get( http://localhost:8800/api/posts/${id} ); setVideo(response.data); setVideoUrl(response.data.video_url); } catch (error) { console.error(error); } };

    fetchVideo();
    

    }, [id]);

    useEffect(() => { const fetchComments = async () => { try { const response = await axios.get( http://localhost:8800/api/posts/comments/${id} ); setComments(response.data); } catch (error) { console.error(error); } };

    fetchComments();
    

    }, [id]);

    const handleSubmit = async (e) => { e.preventDefault(); console.log(commentText); // TODO: Handle submitting the comment };

    const handleDelete = async (e) => { e.preventDefault(); // TODO: Handle deleting the post };

    if (!video) { return Loading...; }

    return ( {video.title} Published by {video.user_id} on{" "} {video.publishedDate} <source src={http://localhost:8800/api/${videoUrl}} type="video/mp4" />

      <section id="comments">
        <h2>Comments</h2>
        <form onSubmit={handleSubmit}>
          <label htmlFor="comment">Add a Comment:</label>
          <textarea
            id="comment"
            name="comment"
            value={commentText}
            onChange={(e) => setCommentText(e.target.value)}
          ></textarea>
          <button type="submit">Submit Comment</button>
          <button onClick={handleDelete}>Delete posts</button>
        </form>
        <ul>
          {comments.map((c) => (
            <li key={c.id}>
              <p>
                <a href="profile.html">{c.user_id}</a> said:
              </p>
              <p>{c.comment}</p>
            </li>
          ))}
        </ul>
      </section>
    </div>
    

    ); }

    export default View; ]

  2. Assistant

    It seems like the response you are receiving for comments is not an array, but you are treating it as an array in the comments.map function. First, make sure that the response you are getting is an array. You can try to console.log() the response.data to see its structure. If the response is not an array, manipulate it to convert it into an array before setting it to the comments state.

    useEffect(() => {
      const fetchComments = async () => {
        try {
          const response = await axios.get(
            `http://localhost:8800/api/posts/comments/${id}`
          );
          console.log("Comments response:", response.data); // Add this line to check the response structure
    
          // Manipulate the response here if it's not an array
          const commentsArray = response.data; // Replace this with the actual array extracted from response
    
          setComments(commentsArray);
        } catch (error) {
          console.error(error);
        }
      };
    
      fetchComments();
    }, [id]);
    

    If the response is an array but you're still getting the error, make sure that the initial state for comments is an empty array ([]).