javascript 当在react(nextjs)中尝试单独打印对象的属性时,对象作为整体打印,但未定义打印

kg7wmglp  于 2023-10-14  发布在  Java
关注(0)|答案(1)|浏览(76)

在我的next.js web应用中-

interface NavbarProps {
    currentUser: {
        id?: String
        username?: string 
        name?: string
   }
}
const Navbar: React.FC<NavbarProps> = ({ currentUser }) => {
    console.log(currentUser)
    console.log(currentUser?.id )

在上面的代码中,当我尝试打印currentUser时,它会按预期打印,即。

{
  currentUser: {
    id: '65255530a08db2db2f91e7e9',
    email: '[email protected]',
    emailverified: null,
    hashedPassword: 'xyz',
    username: 'abc',
    name: 'abc',
    bio: null,
    profilepic: null,
    
  }
}

但当我尝试console.log的id;它总是打印为“undefined”

我从这里传递currentUser-

import Navbar from '@/components/headersAndFooters/navbar'
import { serverAuth } from '@/lib/serverAuth'
import React from 'react'

interface LayoutProps {
    children: React.ReactNode
}


const layout: React.FC<LayoutProps> = async({ children }) => {
    let currentUser = await serverAuth()
     
     
    return (
        <div>
            <Navbar currentUser={currentUser as any}/>
            {children}
        </div>
    )
}

export default layout;

我试图在删除.next文件后重新启动服务器;甚至尝试在不同的浏览器;我还尝试使用.jsx将其打印到屏幕上

mf98qq94

mf98qq941#

您的对象与声明不同
应为:

const layout: React.FC<LayoutProps> = async({ children }) => {
    let data = await serverAuth()
     
     
    return (
        <div>
            <Navbar currentUser={data.currentUser as any}/>
            {children}
        </div>
    )
}

此外,尽量避免使用任何

相关问题