首页 > 解决方案 > nextjs console.log 在函数返回函数中不起作用

问题描述

我正在玩 nextjs,但我无法调试这样的函数:

export const authInitialProps = isProtectedRoute => {
  console.log("mylog");// this works 
  return ({ req, res }) => {
    console.log("inner my log", req); // this doesn't work
  };
};

在页面中使用

ProfilePage.getInitialProps = async () => {
  const auth = authInitialProps(true);
  if (!typeof auth === "function") {
    const user = await getUserProfile();
    return { user };
  }
  return { user: null };
};

我从来没有在 chrome 控制台和我的控制台终端中看到“我的日志内部”。请问有什么问题吗?

标签: next.js

解决方案


试试这个它可能工作:

ProfilePage.getInitialProps = async () => {
  const auth = await authInitialProps(true); // await added
  if (!typeof auth === "function") {
    const user = await getUserProfile();
    return { user };
  }
  return { user: null };
};

我认为不通过它应该是异步的!


推荐阅读