简体   繁体   English

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

[英]Unhandled Rejection with FieldValue.arrayUnion()

I'm working on making a social media clone and when I'm trying to follow another profile I'm getting thrown an error.我正在制作一个社交媒体克隆,当我尝试关注另一个个人资料时,我遇到了错误。 When I try to console.log("follow user") it'll work just fine but when I change it to onClick={handleFollowUser} it'll throw an error saying that something is undefined.当我尝试 console.log("follow user") 时,它会正常工作,但是当我将其更改为 onClick={handleFollowUser} 时,它会抛出一条错误消息,指出某些内容未定义。

You can view the specific error here: https://imgur.com/Lit4iqv具体错误可以在这里查看: https://imgur.com/Lit4iqv

The error I get is:我得到的错误是:

Unhandled Rejection (FirebaseError): Function FieldValue.arrayUnion() called with invalid data.未处理的拒绝 (FirebaseError):Function 使用无效数据调用 FieldValue.arrayUnion()。 Unsupported field value: undefined (found in document users/pX1fay49L6fLiYwnzIXL)不支持的字段值:未定义(在文档 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)
      });
  }

As @Frank van Puffelend said, you're trying to write an undefined value to your database and this will trow an error.正如@Frank van Puffelend所说,您正试图向数据库写入一个未定义的值,这会引发错误。 You can fix in several ways, one is to make Firebase ignore the undefined values.您可以通过多种方式修复,一种是让 Firebase 忽略未定义的值。 An example is:一个例子是:

import * as firestore from "firebase-admin"

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

暂无
暂无

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

相关问题 将 Flutter 中的 FieldValue.arrayUnion 与 Cloud FireStore 一起使用 - Use FieldValue.arrayUnion in Flutter with Cloud FireStore firebase.firestore.FieldValue.arrayUnion 不是 function - firebase.firestore.FieldValue.arrayUnion is not a function [未处理的 promise 拒绝:ReferenceError:找不到变量:auth] - [Unhandled promise rejection: ReferenceError: Can't find variable: auth] 可能未处理的 Promise 拒绝 (id:0) | 反应原生 firebase 谷歌登录 - Possible Unhandled Promise Rejection (id:0) | React native firebase google login [未处理的 promise 拒绝:FirebaseError:Function addDoc() 使用无效数据调用。 不受支持的字段值:自定义 Class object - [Unhandled promise rejection: FirebaseError: Function addDoc() called with invalid data. Unsupported field value: a custom Class object Firebase Flutter arrayUnion 什么都不做 - Firebase Flutter arrayUnion not doing anything 如何在 Firebase Firestore 中使用 arrayUnion() 将 object 推送到数组中? - How to push an object in an array with arrayUnion() in Firebase Firestore? FieldValue.increment 不起作用但添加“操作数” - FieldValue.increment does not work but adds "operand" 使用 FieldValue 或 FirestoreAdminClient 时未定义 - Getting undefined when using FieldValue or FirestoreAdminClient 如何使用 arrayUnion (TypeScript) 将字符串数组 append 转换为 Firebase 中的数组 - How to append an array of strings to an array in Firebase using arrayUnion (TypeScript)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM