firestore: user Array.includes with a DocumentReference



我正在尝试检查具有DocumetReferences的数组是否包含特定的引用

这是我的尝试:

const likedWorkouts = user.collection('additional').doc('liked');
const snapshot = await likedWorkouts.get();
const exists = snapshot.exists;
hasLiked = snapshot.data()?.liked?.includes(workout); // This part (workout is a DocumentReference)

DocumentReference是一个对象,不能直接比较它们。您可以使用isEqual()方法来比较参考:

hasLiked = !!snapshot.data()?.liked?.find((likeRef) => likeRef.isEqual(workout));
// Alternatively, you can compare the paths
// likeRef.path === workout.path

新模块化SDK中的DocumentReference没有isEqual()方法,而是有一个顶级函数refEqual()。它可以如下使用:

import { refEqual } from "firebase/firestore";
hasLiked = !!snapshot.data()?.liked?.find((likeRef) => refEqual(likeRef, workout));

有关对象的详细信息,请签出MDN。

最新更新