首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >普莉丝玛,TS和getServerSideProps,我被困住了

普莉丝玛,TS和getServerSideProps,我被困住了
EN

Stack Overflow用户
提问于 2021-11-25 21:02:52
回答 2查看 575关注 0票数 1

我试图在我的Nextjs应用程序中通过Prisma进行一个简单的查询,没有错误,但是无法得到任何结果,而且console.log不在my: getServerSideProps .

我遗漏了什么?CasinoComment表中有数据(通过axios提交/添加),我可以通过prisma查看这些数据,而且我没有看到任何错误.

comments.tsx

代码语言:javascript
复制
import { GetServerSideProps, GetStaticProps } from "next";
import { signIn, signOut, useSession } from "next-auth/react";
import { useRef, useState } from "react";

import { CasinoComment } from "@prisma/client";
import prisma from "../../lib/prisma";

type Props = {
  comments?: CasinoComment;
};

export default function Comment({ comments }: Props) {
 
  console.log(comments);
  ...
}


export const getServerSideProps: GetServerSideProps = async ({ params }) => {
  console.log("test222");

  const comments = await prisma.casinoComment.findMany();
  console.log(comments);
  return {
    props: { comments },
  };
};

schema.prisma

代码语言:javascript
复制
datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

generator client {
  provider        = "prisma-client-js"
}

model User {
  id            String    @id @default(cuid())
  name          String?
  email         String?   @unique
  emailVerified DateTime?
  image         String?
  accounts      Account[]
  sessions      Session[]
  casino_comments CasinoComment[]
}


model CasinoComment {
  id          Int         @id @default(autoincrement())
  casinoId    Int
  content     String   @db.VarChar(2000)
  reply       Boolean?
  parentId    Int?
  approved    Boolean
  likes       Int?
  createdAt   DateTime @default(now())
  updatedAt   DateTime @default(now()) @updatedAt
  author      User @relation(fields: [authorId], references: [id])
  authorId    String
}

lib/prma.js

代码语言:javascript
复制
import { Prisma, PrismaClient } from "@prisma/client";

declare global {
  namespace NodeJS {
    interface Global {
      prisma: PrismaClient;
    }
  }
}

let prisma: PrismaClient;

if (typeof window === "undefined") {
  if (process.env.NODE_ENV === "production") {
    prisma = new PrismaClient();
  } else {
    if (!global.prisma) {
      global.prisma = new PrismaClient();
    }

    prisma = global.prisma;
  }
}

export default prisma;
EN

回答 2

Stack Overflow用户

发布于 2021-11-26 23:09:22

这对我很有用。

代码语言:javascript
复制
export default function Comment(props: any) {
 
  console.log("Props are",  props); // to see data
  ...
}


export async function getServerSideProps() {
    const comments = await prisma.casinoComment.findMany();
    return {
      props: {
        comments: comments
      },
    };
  }
票数 1
EN

Stack Overflow用户

发布于 2021-11-26 23:19:20

问题是我试图在组件中使用getServerSideProps :/学到的东西!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70117202

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档