0

我正在使用next-iron-sessionandnext-redux-wrapper在我的 nextjs 客户端中。它们都要求我用它们各自的函数包装 getServerSideProps。有人知道,我如何链接包装器?

下一届铁杆会议(个人)

export const getServerSideProps = 
  withIronSession(
  async ({ req, res }) => {
    const user = req.session.get("user");

    if (!user) {
      res.statusCode = 404;
      res.end();
      return { props: {} };
    }

    return {
      props: { user }
    };
  },
  {
    cookieName: "MYSITECOOKIE",
    cookieOptions: {
      secure: process.env.NODE_ENV === "production" ? true : false
    },
    password: process.env.APPLICATION_SECRET
  }
);

next-redux-wrapper(个人)

export const getServerSideProps = wrapper.getServerSideProps((store) => async () => {
  const user = store.getState().user
  console.log('user', user)
})

组合(无效)

TypeError:无法读取未定义的属性“ttl”

    export const getServerSideProps = withIronSession(
      wrapper.getServerSideProps(
        (store) =>
          async ({ req, res }) => {
            const user = req.session.get('user')
            console.log(user)
            return {
              props: { user: 'hi' },
            }
          },
        {
          cookieName: 'MYSITECOOKIE',
          cookieOptions: {
            secure: process.env.NODE_ENV === 'production' ? true : false,
          },
 password: process.env.APPLICATION_SECRET
        },
      ),
    )
4

1 回答 1

0

我不知道为什么它突然起作用了。那是我的第一个解决方案,现在已经不起作用了。但是对于较新的,请...

export const getServerSideProps = wrapper.getServerSideProps((store) =>
  withIronSession(
    async ({ req, res }) => {
      const user = await req.session.get('user')
      console.log('uer', user)
      return {
        props: { user: 'hi' },
      }
    },
    {
      cookieName: 'MYSITECOOKIE',
      cookieOptions: {
        secure: process.env.NODE_ENV === 'production' ? true : false,
      },
      password: process.env.APPLICATION_SECRET,
    },
  ),
)
于 2021-08-17T09:02:19.280 回答