1

I have object from postgresql:

enter image description here

Need to get id, name and content on NextJS component like that:

export async function getServerSideProps() {
  const data = await prisma.note.findFirst()
  return {
    props: {
      userNote: data,
    },
  }
}
interface INote {
  id: number
  name: string
  content: string
}

const Home = ({ name, content, id }: INote) => {
  console.log(name)
  return <div>hello world</div>
}

but i got undefined. What's wrong?

3
  • How you're rendering this Home component? Commented Nov 10, 2021 at 19:38
  • "Home" is main component in pages/index.tsx Commented Nov 10, 2021 at 19:41
  • I can get this object if = (userNote: any) => { ... } but it's wrong Commented Nov 10, 2021 at 19:42

1 Answer 1

2

The problem is that your props in the Home component are not

{
  id: number
  name: string
  content: string
}

but actually,

{
 userNote: {
  id: number
  name: string
  content: string
 }
}

You'll either need to change your destructuring and the type:

const Home = ({ userNote: { name, content, id } }: { userNote: INote }) => {

or you could change your getServerSideProps:

export async function getServerSideProps() {
  const data = await prisma.note.findFirst()
  return {
    props: data,
  }
}

In my experience it's more idiomatic to go with the first approach and change it to:

export async function getServerSideProps() {
  const data = { id: 1, name: 'test', content: 'content' }
  return {
    props: {
      userNote: data,
    },
  }
}

interface INote {
  id: number
  name: string
  content: string
}

interface HomeProps {
   userNote: INote
}

const Home = ({ userNote: { name, content, id } }: HomeProps) => {
  console.log(name)
  return <div>hello world</div>
}

export default Home

Sign up to request clarification or add additional context in comments.

1 Comment

thank you, it works now!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.