Home > OS >  Update firebase document v9 not working with query
Update firebase document v9 not working with query

Time:11-30

My current issue while using react native expo and firebase is after the user picks their user profile photo through expo image picker, and gets the url, i want to set the photoURL of the user document in my users database to the url that i get from the react native image picker library

heres the code: (imgFirebase() is where the problem is located)

const pickImage = async () => {
    try {
 // No permissions request is necessary for launching the image library
    let result = await ImagePicker.launchImageLibraryAsync({
      mediaTypes: ImagePicker.MediaTypeOptions.All,
      allowsEditing: true,
      aspect: [4, 3],
      quality: 1,
    });


    if (!result.canceled) {
      auth.currentUser.photoURL = result.uri
      setImage(result.uri);
      imgFirebase()
    }
    }
    catch(E) {
      alert(E)
    }

  };
  async function imgFirebase () {
   const d = await fetch(image)
   const dd = await d.blob()
   const fileName = image.substring(image.lastIndexOf("/") 1)
   const storage = getStorage();
  const storageRef = ref(storage, fileName);
  uploadBytes(storageRef,dd).then((snapshot) => {
    getDownloadURL(snapshot.ref).then(async (url) => {

      // Create a query against the collection.
      setUrl(url)
      
      const users = collection(db, "users");

      // Create a query against the collection.
      const q = query(users, where("uid", "==", uid));
      const querySnapshot = await getDocs(q);
querySnapshot.forEach((doc) => {
  async function upadad() {
    try {
      updateDoc(q, {
        photoUrl: url
     })
     console.log(doc.data())
    }
    catch(e) {
      alert(e)
    }
  }

  
});
      
}).catch(e=>{
  alert(e)
}) 
  }); 
  }

i have seen lots of documentation on the updateDoc function but none of them are for firebase version 9 and with compound queries.

this is the main issue itself:

    const users = collection(db, "users");

      // Create a query against the collection.
      const q = query(users, where("uid", "==", uid));
      const querySnapshot = await getDocs(q);
querySnapshot.forEach((doc) => {
  async function upadad() {
    try {
      updateDoc(q, {
        photoUrl: url
     })
     console.log(doc.data())
    }
    catch(e) {
      alert(e)
    }
  }

what would be the appropriate syntax to successfully update the photoUrl in the users document when they pick a image and get a url?

firestore structure

CodePudding user response:

Something like this should work:

const users = collection(db, "users");

const q = query(users, where("uid", "==", uid));
const querySnapshot = await getDocs(q);

querySnapshot.forEach((doc) => {
  updateDoc(doc.ref, { //            
  • Related