【问题标题】:Cannot destructure property 'name' of 'response.profileObj' as it is undefined无法解构“response.profileObj”的属性“名称”,因为它未定义
【发布时间】:2022-06-20 05:37:33
【问题描述】:
const navigate = useNavigate();

  const responseGoogle = (response) => {

    localStorage.setItem('user', JSON.stringify(response.profileObj));

    const { name, googleId, imageUrl } = response.profileObj;

    const doc = {
      _id: googleId,
      _type: 'user',
      userName: name,
      image: imageUrl,
    };

    client.createIfNotExists(doc).then(() => {
      navigate('/', { replace: true });
    });
  };

我收到“名称未定义”的错误 - 但如何? imageUrlgoogleId 工作正常,那么 name 有什么问题?

【问题讨论】:

  • 你能分享你的回应吗?对象会有所帮助
  • 我如何分享@FaisalAhmed
  • 我怀疑googleIdimageUrl 是否“工作正常”。您只是收到 name 的错误,因为它是它尝试解构的第一个属性并且它已经在那里失败了。此错误消息告诉您 response.profileObjundefined 因此无法解构...

标签: reactjs google-api google-authentication


【解决方案1】:

这可能是一个符号问题,试试这个吧:

const navigate = useNavigate();

const responseGoogle = (response) => {

localStorage.setItem('user', JSON.stringify(response.profileObj));

const doc = {
  _id: response.profileObj.googleId,
  _type: 'user',
  userName: response.profileObj.name,
  image: response.profileObj.imageUrl,
};

client.createIfNotExists(doc).then(() => {
  navigate('/', { replace: true });
});

};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-14
    • 2021-10-20
    • 2020-10-07
    • 1970-01-01
    • 1970-01-01
    • 2021-03-08
    相关资源
    最近更新 更多