0

I'm stuck trying to fetch comments for a specific post, currently fetching returns all the comments on the site. Olny need posts for the specific post id.

[slug].js I'm using const { data: commentList, error } = useSWR(`/api/comments/`, fetcher);

and in my comments.js (api)

  switch (req.method) {
// get all comments for specific event_id
    case "GET":
        const { data, error } = await supabase
            .from("comments")
            .select("*, user_id (username, avatar_url)")
            .order("created_at", { ascending: false })      
        if (error) {
            res.status(500).json({ error: error.message });
        } else {
            res.status(200).json(data);
        }
        break;```

I've tried adding .eq but it wont work.
PDANGER
  • 87
  • 11
  • 1
    did you try to use a dynamic api route? something like `/comments/[postId]` ? so you can pass the `postId` to the endpoint – mocherfaoui Oct 22 '22 at 10:30
  • Adding eq is the way to go. In what way did it not work? Could you share the code with eq that you tried? – dshukertjr Oct 24 '22 at 05:55
  • Adding .eq should work here. Could you share more code? For an out-of-the-box integration of swr with supabase, you can check out https://github.com/psteinroe/supabase-cache-helpers (Full Disclosure: I am the author). – psteinroe Nov 08 '22 at 12:11

0 Answers0