Javascript 对象作为子对象无效(找到:[对象承诺])

Javascript 对象作为子对象无效(找到:[对象承诺]),javascript,reactjs,Javascript,Reactjs,我有一个带有渲染路由的私有路由组件,当我使用async/await调用方法时,它返回未捕获不变冲突:对象作为React子对象无效(找到:[object Promise])。我需要改变什么?有一个checkTokenExpirationMiddlewareAdvertiser(),用于验证用户角色并呈现正确的仪表板。当我async/await对用户来说时,承诺似乎没有完全解决 我已尝试从函数中删除async,但无法从函数中获取返回值 const广告客户私有路由=({component:compon

我有一个带有渲染路由的私有路由组件,当我使用
async/await
调用方法时,它返回
未捕获不变冲突:对象作为React子对象无效(找到:[object Promise])
。我需要改变什么?有一个
checkTokenExpirationMiddlewareAdvertiser()
,用于验证用户角色并呈现正确的仪表板。当我
async/await
对用户来说时,承诺似乎没有完全解决

我已尝试从函数中删除
async
,但无法从函数中获取返回值

const广告客户私有路由=({component:component,…rest})=>(
{
console.log(rest)
如果(!loggedInUser())
返回(
);
返回checkTokenExpirationMiddlewareAdvertiser(
()=>,//成功组件
()=>,//故障组件
);
}}
/>
);
export const checkTokenExpirationMiddlewareAdvertiser=async(成功,失败)=>{
const{user}=await loggedInUser();
console.log(用户)
如果(用户){
const{token}=用户;
if(jwtDecode(token).exp
您将
async wait
loggedInUser()
组合使用时不一致

  • checkTokenExpirationMiddlewareAdvertiser
  • 或者将其添加到广告客户专用路线(见下文)
  • 取决于
    loggedInUser()
    是否为异步函数

    const AdvertiserPrivateRoute = async ({ component: Component, ...rest }) => (
      <Route
        {...rest}
        render={props => {
          console.log(rest)
          const userLoggedIn = await loggedInUser();
          if (!userLoggedIn)
            return (
              <Redirect
                to={{ pathname: '/login', state: { from: props.location } }}
              />
            );
          return checkTokenExpirationMiddlewareAdvertiser(
            () => <Component {...props} />, // success component
            () => <AdvertiserError />, // failure component
          );
        }}
      />
    );
    
    const advertizerprivaterout=async({component:component,…rest})=>(
    {
    console.log(rest)
    const userLoggedIn=等待loggedInUser();
    如果(!userLoggedIn)
    返回(
    );
    返回checkTokenExpirationMiddlewareAdvertiser(
    ()=>,//成功组件
    ()=>,//故障组件
    );
    }}
    />
    );
    
    您正在调用
    loggedInUser()
    并在
    checkTokenExpirationMiddlewareAdvertiser
    中使用wait,但在
    广告客户专用路线中不使用。显然,这不是您的错误,但从逻辑上讲,这也没有意义。是的..我如何在广告客户PrivaterRoute中将其命名为Wait?您可以在
    承诺中激发它,然后使用
    链接,或者将
    广告客户PrivaterRoute
    声明为异步常量
    const广告客户PrivaterRoute=async({component:component,…rest}=>…
    嘿,谢谢你,我刚刚从
    checkToken
    中删除了
    async
    ,效果很好。似乎错误来自
    async/await
    的一行字。谢谢,你介意我在这里标记一些东西作为答案(一分钟后),作为答案,就像我解决了你的问题一样?
    const AdvertiserPrivateRoute = async ({ component: Component, ...rest }) => (
      <Route
        {...rest}
        render={props => {
          console.log(rest)
          const userLoggedIn = await loggedInUser();
          if (!userLoggedIn)
            return (
              <Redirect
                to={{ pathname: '/login', state: { from: props.location } }}
              />
            );
          return checkTokenExpirationMiddlewareAdvertiser(
            () => <Component {...props} />, // success component
            () => <AdvertiserError />, // failure component
          );
        }}
      />
    );