访问 Firestore 中具有地图字段的文档。

huangapple go评论61阅读模式
英文:

Accessing a Document that have a Field of Map in firestore

问题

以下是您要翻译的内容:

"I would like to ask on how to access the key value pair in field in firestore via nextjs isReferralAvailable.
The key TESTER1and a boolean value of true I want to display them in a table or just console log the key value pair inside isReferralAvailable

Here is my function to get the ReferralCodes collection and its value.

const [checkReferralStatus, setReferralStatus] = useState([true, false]);
const [referralCodes, setReferralCodes] = useState([]);

const referralCodesDb = (
    collectionName,
    setterName,
    queryOperator,
    valueFieldPrameter
  ) => {
    const q = query(
      collection(db, collectionName),
      where("isReferralAvailable", queryOperator, valueFieldPrameter)
    );
    const unsubscribe = onSnapshot(q, (response) => {
      setterName(
        response.docs.map((data) => {
          return { ...data.data(), id: data.id };
        })
      );
    });
    return unsubscribe;
  };

and I am calling my function in a useEffect

useEffect(() => {
    const unsubscribe = referralCodesDb(
      "ReferralCodes",
      setReferralCodes,
      "in",
      checkReferralStatus
    );
    return () => {
      unsubscribe();
    };
  }, [checkReferralStatus]);

I also created a displayReferralCodes separately from the html so I could easily just call it in a div

const displayReferralCodes = referralCodes
    .slice(pagesVisited, pagesVisited + usersPerPage)
    .map((referralCodes) => {
      // TABLE
      return (
        <tr className="bg-white border-b dark:bg-gray-800 dark:border-gray-700">
          <td className="px-6 py-4 font-medium text-gray-900 whitespace-nowrap dark:text-white">
            {referralCodes.isReferralAvailable}{" "}
          </td>
        </tr>
      );
    });

I am trying to console log the referralCodes however it does not log any values."

英文:

I would like to ask on how to access the key value pair in field in firestore via nextjs isReferralAvailable.
The key TESTER1and a boolean value of true I want to display them in a table or just console log the key value pair inside isReferralAvailable
访问 Firestore 中具有地图字段的文档。

Here is my function to get the ReferralCodes collection and its value.

const [checkReferralStatus, setReferralStatus] = useState([true, false]);
const [referralCodes, setReferralCodes] = useState([]);

const referralCodesDb = (
    collectionName,
    setterName,
    queryOperator,
    valueFieldPrameter
  ) => {
    const q = query(
      collection(db, collectionName),
      where("isReferralAvailable", queryOperator, valueFieldPrameter)
    );
    const unsubscribe = onSnapshot(q, (response) => {
      setterName(
        response.docs.map((data) => {
          return { ...data.data(), id: data.id };
        })
      );
    });
    return unsubscribe;
  };

and I am calling my function in a useEffect

useEffect(() => {
    const unsubscribe = referralCodesDb(
      "ReferralCodes",
      setReferralCodes,
      "in",
      checkReferralStatus
    );
    return () => {
      unsubscribe();
    };
  }, [checkReferralStatus]);

I also created a displayReferralCodes separately from the html so I could easily just call it in a div

const displayReferralCodes = referralCodes
    .slice(pagesVisited, pagesVisited + usersPerPage)
    .map((referralCodes) => {
      // TABLE
      return (
        <tr className="bg-white border-b dark:bg-gray-800 dark:border-gray-700">
          <td className="px-6 py-4 font-medium text-gray-900 whitespace-nowrap dark:text-white">
            {referralCodes.isReferralAvailable}{" "}
          </td>
        </tr>
      );
    });

I am trying to console log the referralCodes however it does not log any values.

答案1

得分: 1

To access fields of Map in a document you can directly use . operator.

In your code:

const referralCodesDb = (
    collectionName,
    setterName,
    queryOperator,
    valueFieldPrameter
  ) => {
    const q = query(
      collection(db, collectionName),
      where("isReferralAvailable", queryOperator, valueFieldPrameter)
    );
    const unsubscribe = onSnapshot(q, (response) => {
      setterName(
        response.docs.map((data) => {
          console.log(data.data().isReferralAvailable.TESTER1); // ⇐ Accessing Fields of Map
          return { ...data.data(), id: data.id };
        })
      );
    });
    return unsubscribe;
  };

As Maps are just a JSON-looking object, hence you can access their fields just like how we access fields of an Object.

英文:

To access fields of Map in a document you can directly use . operator.

In your code :

const referralCodesDb = (
    collectionName,
    setterName,
    queryOperator,
    valueFieldPrameter
  ) => {
    const q = query(
      collection(db, collectionName),
      where("isReferralAvailable", queryOperator, valueFieldPrameter)
    );
    const unsubscribe = onSnapshot(q, (response) => {
      setterName(
        response.docs.map((data) => {
          console.log(data.data().isReferralAvailable.TESTER1); // ⇐ Accessing Fields of Map
          return { ...data.data(), id: data.id };
        })
      );
    });
    return unsubscribe;
  };

As Maps are just an JSON looking object hence you can access their fields just like how we access fields of an Object.

huangapple
  • 本文由 发表于 2023年4月11日 12:19:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75982375.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定