簡體   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