【问题标题】:cb=gapi.loaded_0?le=scs:230 Uncaught TypeError: Cannot destructure property 'name' of 'response.profileObj' as it is undefinedcb=gapi.loaded_0?le=scs:230 Uncaught TypeError: Cannot destruct property \'name\' of \'response.profileObj\' as it is undefined
【发布时间】:2022-11-06 10:33:51
【问题描述】:

我不断收到此错误,我不知道是什么原因造成的。

const Login = () => {
  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 });
    });
  };

如果我做出这个改变const { name, googleId, imageUrl } = response.profileObj ?? {};

然后我明白了:Uncaught Error: createIfNotExists() requires that the document contains an ID ("_id" property)

知道我缺少什么吗?

【问题讨论】:

  • 似乎您的response.profileObj 正在获取nullundefined。当您执行const {name} = {} 时,您将得到undefined,因此您会收到该错误。尝试添加 if 条件并在 profileObj 有数据时执行这些语句
  • 我试过但没有解决问题。我无法使用 google 登录并且不断收到 401 错误。

标签: reactjs react-google-login


【解决方案1】:

当我尝试这样做时,我遇到了同样的错误。就我而言,我发现 googleId 有时为空并导致此类错误。我通过在您的案例中声明 doc const 之前放置一个 IF 语句来解决它,并且只需检查 googleId 是否为空。告诉我这是否适合您,如果不是,我很乐意提供帮助。

【讨论】:

    猜你喜欢
    • 2022-11-20
    • 2017-04-24
    • 2013-01-15
    • 1970-01-01
    • 2019-02-12
    • 1970-01-01
    • 2021-09-20
    • 2021-06-08
    • 1970-01-01
    相关资源
    最近更新 更多