Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array Union is not Defined in Firebase Firestore reactjs

I am trying to let my users upload multiple images at once. Now everything is working correctly, my only problem is that I am not getting the download Urls as an array in my firebase firestore document.

I am using the arrayUnion() functions to combine everything into an array but it gives me this error. "Unhandled Rejection (ReferenceError): arrayUnion is not defined"

What am i doing wrong?

Here is my code:

firebase
      .firestore()
      .collection("Properties")
      .add({
        propname: propName,
        price: price,
        bedrooms: bedroom,
        bathroom: bathroom,
        exclusive: exclusive,
        area: area,
        type: type,
        category: category,
        features: features,
        services: services,
        summary: summary,
        // imageUrls: urls,
        location: location,
        salesAgentID: salesAgent,
        date: getCurrentDate(),
      })
      .then(result => {
        const promises = [];
        Promise.all(
          selectedImages.map(image => {
            const storageRef = storage.ref(
              `propertyImages/${result.id}/${image.name}`
            );
            storageRef.put(image).then(urls => {
              storageRef.getDownloadURL().then(downloadUrls => {
                console.log(downloadUrls);
                firebase
                  .firestore()
                  .collection("Properties")
                  .doc(result.id)
                  .update({
                    propertyID: result.id,
                    images: arrayUnion(downloadUrls),//here is my problem
                  })
                  .then(res => {
                    //handleUploadChange();
                    alert("Property Added Successfully");
                    window.location.reload();
                  });
              });
            });
          })
        );
      });
  
like image 318
motionless570 Avatar asked Sep 23 '26 07:09

motionless570


1 Answers

I solved the problem by using this line of code firebase.firestore.FieldValue.arrayUnion(downloadUrls) that was mentioned in the documentation (documentation link) of Firebase since they upgraded the Firebase version.

like image 53
motionless570 Avatar answered Sep 25 '26 14:09

motionless570



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!