繁体   English   中英

使用 FieldValue.arrayUnion() 未处理的拒绝

[英]Unhandled Rejection with FieldValue.arrayUnion()

我正在制作一个社交媒体克隆,当我尝试关注另一个个人资料时,我遇到了错误。 当我尝试 console.log("follow user") 时,它会正常工作,但是当我将其更改为 onClick={handleFollowUser} 时,它会抛出一条错误消息,指出某些内容未定义。

具体错误可以在这里查看: https://imgur.com/Lit4iqv

我得到的错误是:

未处理的拒绝 (FirebaseError):Function 使用无效数据调用 FieldValue.arrayUnion()。 不支持的字段值:未定义(在文档 users/pX1fay49L6fLiYwnzIXL 中找到)

  async function handleFollowUser() {
    setFollowed(true);
    await updateLoggedInUserFollowing(loggedInUserDocId, profileId, false);
    await updateFollowedUserFollowers(profileDocId, userId, false);
  }

      <button
        className="text-xs font-bold text-blue-medium"
        type="button"
        onClick={handleFollowUser}
        // onClick={console.log('Follow this user!')}
      >
        Follow
      </button>
  export async function updateLoggedInUserFollowing(
    loggedInUserDocId, // currently logged in user document id (gore's profile)
    profileId, // the user that gore requests to follow
    isFollowingProfile // true/false (am i currently following this person?)
  ) {
    return firebase
      .firestore()
      .collection('users')
      .doc(loggedInUserDocId)
      .update({
        following: isFollowingProfile ? FieldValue.arrayRemove(profileId) : FieldValue.arrayUnion(profileId)
      });
  }
  
  export async function updateFollowedUserFollowers(
    profileDocId, // currently logged in user document id (gore's profile)
    loggedInUserDocId, // the user that gore requests to follow
    isFollowingProfile // true/false (am i currently following this person?)
  ) {
    return firebase
      .firestore()
      .collection('users')
      .doc(profileDocId)
      .update({
        followers: isFollowingProfile
          ? FieldValue.arrayRemove(loggedInUserDocId)
          : FieldValue.arrayUnion(loggedInUserDocId)
      });
  }

正如@Frank van Puffelend所说,您正试图向数据库写入一个未定义的值,这会引发错误。 您可以通过多种方式修复,一种是让 Firebase 忽略未定义的值。 一个例子是:

import * as firestore from "firebase-admin"

const db = firestore.firestore();
db.settings({ ignoreUndefinedProperties: true })

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM