NextJs 13 / Prisma在呈现带有数据的页面时出现问题

huangapple go评论65阅读模式
英文:

NextJs 13 / Prisma issues rendering page with data

问题

I'm still learning things so please bear with me.
Using Next 13.4.4
在使用 Next 13.4.4
Set up a postgres database in Vercel
在 Vercel 中设置了一个 Postgres 数据库
Created an api in app src/pages/users as follows
在 app src/pages/users 中创建了一个 API,如下所示

import { prisma } from '@/lib/prisma';
import { NextApiRequest, NextApiResponse } from 'next';

export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
if (req.method === 'GET') {
const users = await prisma.users.findMany();
res.json(users );
}
}
当我访问 http://localhost:3000/api/users 时,我可以看到我的数据,所以根据我有限的知识,这应该是正常的。

This is what my page looks like (app/users/page.tsx)
这是我的页面的样子 (app/users/page.tsx)

'use client';

import { User } from '@prisma/client';
import { useEffect, useState } from 'react';

async function getData() {
const res = await fetch('api/users');
if (!res.ok) {
throw new Error('Failed to fetch data');
}
return res.json();
}

export default async function UsersPage() {
const [users, setUsers] = useState<User[]>([]);

useEffect(() => {
getData().then((users) => {
setUsers(users);
console.log(users);
});
}, []);

return (
<>

List Users

    {users &&
    users.map((user: User) => (

  • {user.name}
  • ))}

</>
);
}

现在我有两个(分开的,我相信?)问题

  1. 当我访问 http://localhost:3000/users 时,我可以在 Dev Tools 中看到用户已被检索到。然而,它们不会显示在我渲染的列表中(我可能需要回到我最近浏览的教程中去)

  2. 当我部署(Vercel)并浏览到页面时,我得到... 不对,我在说谎。刚刚检查了一下,我得到了与本地相同的问题...我可以发誓它一直在说 PrismaClient 无法在浏览器中运行。现在我有点困惑了。

如果有人愿意批评我的代码,我对任何建设性的批评都持开放态度。事情与我以前使用的 Angular 完全不同(虽然我也不能称自己是那里的专家)。

英文:

I'm still learning things so please bear with me.
Using Next 13.4.4
Set up a postgres database in Vercel
Created an api in app src/pages/users as follows

import { prisma } from &#39;@/lib/prisma&#39;;
import { NextApiRequest, NextApiResponse } from &#39;next&#39;;

export default async function handler(
  req: NextApiRequest,
  res: NextApiResponse
) {
  if (req.method === &#39;GET&#39;) {
    const users = await prisma.users.findMany();
    res.json(users );
  }
}

When I go to http://localhost:3000/api/users I can see my data so based on my limited knowledge that's ok

This is what my page looks like (app/users/page.tsx)

&#39;use client&#39;;

import { User} from &#39;@prisma/client&#39;;
import { useEffect, useState } from &#39;react&#39;;

async function getData() {
  const res = await fetch(&#39;api/users&#39;);
  if (!res.ok) {
    throw new Error(&#39;Failed to fetch data&#39;);
  }
  return res.json();
}

export default async function UsersPage() {
  const [users, setUsers] = useState&lt;User[]&gt;([]);

  useEffect(() =&gt; {
    getData().then((users) =&gt; {
      setUsers(users);
      console.log(users);
    });
  }, []);

  return (
    &lt;&gt;
      &lt;h1&gt;List Users&lt;/h1&gt;
      &lt;ul&gt;
        {users &amp;&amp;
          users.map((user: User) =&gt; (
            &lt;li key={user.id}&gt;{user.name}&lt;/li&gt;
          ))}
      &lt;/ul&gt;
    &lt;/&gt;
  );
}

Now I have 2 (separate I believe?) issues

  1. When I go to http://localhost:3000/users I can see the users been retrieved using Dev Tools. However they dont show up in the page in the list I'm rendering (I probably have to go back to the tutorials I've been skimming over lately)

  2. When I deploy (vercel) and browse to the page I get ... no way, I'm lying. Just checked and I get the same issue as local...Can swear it been saying PrismaClient is unable to be run in the browser. Confused now

And if anyone would mind crtisizing my code, I'm open to any constructive critic. Things are so different from Angular where I came from (though can't call myself an expert there either)

答案1

得分: 1

  1. setProviders 更改为 setUsers
  2. PrismaClient 无法在浏览器中运行

可能的问题是:

'use client';
import { User} from '@prisma/client';

这可能会将 @prisma/client 打包进去。

你可以尝试只导入类型:

'use client';
import type { User} from '@prisma/client';

或者在客户端组件内部不导入 @prisma/client 的任何内容。

英文:
  1. setProviders to setUsers
  2. PrismaClient is unable to be run in the browser

Possible the issue is:

&#39;use client&#39;;
import { User} from &#39;@prisma/client&#39;;

This may pull @prisma/client into bundle.

you can try to import only type

&#39;use client&#39;;
import type { User} from &#39;@prisma/client&#39;;

or do not import anything from @prisma/client inside client component.

huangapple
  • 本文由 发表于 2023年6月13日 02:50:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76459492.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定