Home > Net >  Reactjs variable not defined error when declared inside conditional statement
Reactjs variable not defined error when declared inside conditional statement

Time:10-09

I am defining my const/var in a if condition and using it but it still says that its not defined. the var/const is userfinal:

const uploadPostOnline = async (imageUrl, caption) => {
  const userId = auth.currentUser.uid;
  console.log(userId);
  const collectionRef = collection(db, "posts");

  const querySnapshot = await getDocs(collection(db, "realusers"));
  querySnapshot.forEach((doc) => {
    if (doc.data().owner_uid === userId) {
      var userfinal = doc.data().username;
      console.log(doc.data().username);

    }

  });

  addDoc(collectionRef, {
    imageUrl,
    caption,
    username: userfinal,(NOTE: it says userfinal is not defined)
    likes: 0,
    likesByUsers: [],
    comments: [],
    createdAt: serverTimestamp()
  });
};

CodePudding user response:

This should work:

let userfinal; //            
  • Related