There are questions and user wanted to add new Categories in those questions. Categories like in Examination Institute and Year.
After user select questions and fields to add new categories. It first check each document in database to make sure field is not present then It creates two arrays (one for each category). After that data can be updated. I'm using async functions and for some reason validateData()
return empty array for EITemp
and YRTemp
on First call. I have to click button twice to get array with requried data.
Main Code:
const saveData = async () => {
//Firebase Firestore Batch to Write Multiple Documents
const batch = writeBatch(firestore);
if (EI.length > 0) {
const docs = Array.from(new Set(EI))
const EIValue = selectedFields.examInstitute
docs.forEach(docID => {
const DocRef = doc(firestore, "multichoice", docID);
batch.update(DocRef, { ["ExamInstitute"]: arrayUnion(EIValue) });
batch.update(DocRef, { ["Updated"]: timestamp });
});
}
if (YR.length > 0) {
const docs = Array.from(new Set(YR))
const YRValue = selectedFields.year
docs.forEach(docID => {
const DocRef = doc(firestore, "multichoice", docID);
batch.update(DocRef, { ["Year"]: arrayUnion(YRValue) });
batch.update(DocRef, { ["Updated"]: timestamp });
});
}
try {
await batch.commit()
}
catch (error) { console.log(error) }
}
const validateData = async () => {
setSearching(true)
const ei = selectedFields.examInstitute;
const yr = selectedFields.year
var EITemp = []
var YRTemp = []
for await (const doc of selectedDocsID) {
const response = await fetchDoc("multichoice", doc, false)
const EIField = response["ExamInstitute"]
const YRField = response["Year"]
if (EIField == undefined) EITemp.push(doc)
else if (EIField.includes(ei) != true) EITemp.push(doc)
if (YRField == undefined) YRTemp.push(doc)
else if (YRField.includes(yr) != true) YRTemp.push(doc)
}
setEI(EITemp)
setYR(YRTemp)
console.log(EI)
console.log(YR)
await saveData()
}
return (
<>
<Button disabled={searching} onClick={() => validateData()}>Update Data</Button>
</>
);
Fetch Doc Function:
export const fetchDoc = async (collection, slug, Seralize = true) => {
const docRef = doc(firestore, collection, slug);
const docSnap = await getDoc(docRef)
if (docSnap.exists()) { return Seralize ? postToJSON(docSnap) : docSnap.data() }
else { console.log("No such document!"); }
};