safak / next-blog

316 stars 318 forks source link

Error: data?.map is not a function HELP!! #26

Open ayd0gdu opened 11 months ago

ayd0gdu commented 11 months ago

Ekran görüntüsü 2023-11-01 053340

ayd0gdu commented 11 months ago

These are my codes:

import React from "react";
import styles from "./categoryList.module.css";
import Link from "next/link";
import Image from "next/image";

const getData = async () => {
  const res = await fetch("http://localhost:3000/api/categories", {
    cache: "no-store",
  });

  if (!res.ok) {
    throw new Error("Failed");
  }

  return res.json();
};

const CategoryList = async () => {
  const data = await getData();
  return (
    <div className={styles.container}>
      <h1 className={styles.title}>Popular Categories</h1>
      <div className={styles.categories}>
        {data?.map((item) => (
          <Link
            href={"/blog?cat=style"}
            className={`${styles.category} ${styles[item.slug]}`}
            key={item._id}
          >
            {item.img && (
              <Image
                src={item.img}
                alt=""
                width={32}
                height={32}
                className={styles.image}
              />
            )}
            {item.title}
          </Link>
        ))}
      </div>
    </div>
  );
};

export default CategoryList;
allanmathenge commented 11 months ago

Look at the categories route.js, the problem is in fetching and passing that data. For example your category request should be ### const categories = await prisma.category.findMany();

shantanuchavhan commented 11 months ago

Look at the categories route.js, the problem is in fetching and passing that data. For example your category request should be ### const categories = await prisma.category.findMany();

hii i am gettig empty prisma value

Bdisha123 commented 4 months ago

Look at the categories route.js, the problem is in fetching and passing that data. For example your category request should be ### const categories = await prisma.category.findMany();

hii i am gettig empty prisma value

same...have you found any solution?

starlove54 commented 3 months ago

This may happen if you dont receive any data from db incase you deployed on the server make sure to enter you server ip address in network access of mongodb, if not then you need to put your current ip address in network access of mongodb to receive data, or you could go with the option of any ( doesn't matter the ip address; you will recieve data but it is a security risk so change it to server ip address after deployment),