简体   繁体   English

对象无效,无法作为React子对象(找到:[object Promise])

[英]Objects not valid as a React child (found: [object Promise])

I have a private route component with renders a route, it returns Uncaught Invariant Violation: Objects are not valid as a React child (found: [object Promise]) when I call a method with async/await . 我有一个渲染路径的私有路由组件,它返回Uncaught Invariant Violation: Objects are not valid as a React child (found: [object Promise])当我使用async/await调用方法时, Uncaught Invariant Violation: Objects are not valid as a React child (found: [object Promise]) What do I need to change? 我需要更改什么? There is a checkTokenExpirationMiddlewareAdvertiser() that verifies the role of a user and renders the right dashboard. 有一个checkTokenExpirationMiddlewareAdvertiser()可以验证用户的角色并呈现正确的仪表板。 It seems when I async/await for the user the promise doesn't resolve completely. 似乎当我async/await用户时,诺言无法完全解决。

I have tried removing the async from the function but then i can't get the return value from the function. 我试图从函数中删除async ,但后来我无法从函数中获取返回值。

const AdvertiserPrivateRoute = ({ component: Component, ...rest }) => (
  <Route
    {...rest}
    render={props => {
      console.log(rest)
      if (!loggedInUser())
        return (
          <Redirect
            to={{ pathname: '/login', state: { from: props.location } }}
          />
        );
      return checkTokenExpirationMiddlewareAdvertiser(
        () => <Component {...props} />, // success component
        () => <AdvertiserError />, // failure component
      );
    }}
  />
);
export const checkTokenExpirationMiddlewareAdvertiser = async (success, fail) => {
  const { user } = await loggedInUser();
  console.log(user)
  if (user) {
    const { token } = user;
    if (jwtDecode(token).exp < Date.now() / 1000) {
      removeUser();
      return fail();
    }
    if (user.role !== 'advertiser') return fail();
    console.log('here');
    return success();
  }
  console.log("here")
  return fail();
};

Your usage of async await in combination with loggedInUser() is inconsistent. loggedInUser()结合使用的async await不一致。

  1. Either remove it from checkTokenExpirationMiddlewareAdvertiser 可以将其从checkTokenExpirationMiddlewareAdvertiser删除
  2. Or add it to AdvertiserPrivateRoute (see below) 或将其添加到AdvertiserPrivateRoute (请参见下文)

depending on whether loggedInUser() is an asynchronous function 取决于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
      );
    }}
  />
);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 对象作为 React 子级无效(发现:[object Promise]) - Objects are not valid as a React child (found: [object Promise]) React 错误“对象作为 React 子项无效(找到:[object Promise])” - React error "Objects are not valid as a React child (found: [object Promise])" 对象在反应渲染中作为 React 子级无效(找到:[object Promise]) - Objects are not valid as a React child (found: [object Promise]) in react render 错误:对象在 reactjs 中作为 React 子对象无效(找到:[object Promise]) - Error: Objects are not valid as a React child (found: [object Promise]) in reactjs Redux:对象作为 React 子项无效(找到:[object Promise]) - Redux: Objects are not valid as a React child (found: [object Promise]) 对象作为尝试返回 swal 的 React 子项(找到:[object Promise])无效 - Objects are not valid as a React child (found: [object Promise]) trying to return swal 解决功能组件中的“对象作为 React 子级无效(找到:[object Promise])” - Resolving “Objects are not valid as a React child (found: [object Promise])” in Functional Components 我收到一个错误:对象作为 React 子对象无效(找到:[object Promise]) - I got an Error: Objects are not valid as a React child (found: [object Promise]) 如何修复错误:对象作为 React 子对象无效(找到:[object Promise]) - How to fix Error: Objects are not valid as a React child (found: [object Promise]) 对象作为 React 子对象无效 [object Promise] - Objects are not valid as a React child [object Promise]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM