【问题标题】:Get user.id as a string from clerk从职员那里获取 user.id 作为字符串
【发布时间】:2023-02-07 05:19:22
【问题描述】:

我需要获取职员 ID 以与后端核对,但职员使用的钩子在此类函数中不起作用

export const getServerSideProps = async ({ req }) => {

  const { isLoaded, isSignedIn, user } = useUser();

 

  const userman = user.id;

 

  const posts = await prisma.kalender.findMany({

    where:{accountid: userman}

  })

  await prisma.$disconnect();

  return { props: { posts: JSON.parse(JSON.stringify(posts))  } }

}

这就是我希望它工作的方式。有没有办法将 user.id 作为字符串获取? 谢谢!

【问题讨论】:

    标签: reactjs typescript next.js clerk


    【解决方案1】:

    您可以将 user.id 转换为 string。

    user.id.toString() 或String(user.id),如果定义了user.id,两者的行为相同。

    参考:

    【讨论】:

      【解决方案2】:

      您可以使用类型断言。

      1. 在UseUser 上输入断言。

      首先新建一个类型(export const以上):

      interface UseUserT {
        isLoaded: boolean;
        isSignedIn: boolean;
        user: { id: string };
      }
      

      然后把这个类型赋值给useUser:

      const { isLoaded, isSignedIn, user } = useUser() as UseUserT;
      

      2. 在user.id 上输入断言。

      const userman = user.id as string;
      

      【讨论】:

        猜你喜欢
        • 2011-08-10
        • 2020-02-20
        • 2021-02-16
        • 1970-01-01
        • 1970-01-01
        • 2011-12-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多