I have a code below using if/else condition to evaluate what value to assign addedDocument based on whether customId is not Null.
Why is it that addedDocument cannot be found as seen in the picture below?
if (customId) {
const addedDocument = await setDoc(doc(ref), {
...documentData,
createdAt,
});
} else {
const addedDocument = await addDoc(ref, { ...documentData, createdAt });
}
dispatchIfNotCancelled({
type: 'ADDED_DOCUMENT',
payload: addedDocument,
});
if/else operator causing variable to not be found
But can be found when using a ternary operator? As seen in the picture below
const addedDocument = customId
? await setDoc(doc(ref, customId), { ...documentData, createdAt })
: await addDoc(ref, { ...documentData, createdAt });
dispatchIfNotCancelled({
type: 'ADDED_DOCUMENT',
payload: addedDocument,
});