英文:
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 TESTER1
and 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 TESTER1
and 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.
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论