0

I would like to ask if is it possible to add data in a nested array.The result i want is this enter image description here

But i get this when i add a new rating with the code i use

enter image description here

async function getAll(){
    const userEmail= firebase.firestore().collection('users')
    const snapshot=await userEmail.where('email','==',index.email).get()
    if (snapshot.empty) {
      console.log('No matching documents.');
      return;
    }  
    
    snapshot.forEach(doc => {
      userEmail.doc(doc.id).set({userRatings2:firebase.firestore.FieldValue.arrayUnion({parking:[markerName]})},{merge:true})
      userEmail.doc(doc.id).set({userRatings2:firebase.firestore.FieldValue.arrayUnion({rating:[currentRating]})},{merge:true})

     
      
     console.log(userEmail.doc(doc.id));
    });
  }
  getAll()
1
  • can you add your updated code in this thread? Commented Jan 4, 2023 at 7:31

2 Answers 2

1

Unfortunately in Firebase Firestore you can't even change a simple Array value at a specific index. That means also that you can't save nested array values. You can read more about it on this answer.

The only way to do it is to download the whole Array make your modifications and save the whole array again to the databse. There is not much context of your app so I can't tell if that is a good solution for you. It depends how much data you have in the array.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes i've tried this way and it works fine. Thanks a lot!
0

I've managed to do it by using forEach function to an array of image path

  const [imageUri, setImageUri] = useState([]);
  const [uploading, setUploading] = useState(false);

  const UploadImage = () => {
    setUploading(true);
    imageUri.forEach(async (image, index) => {
      // setTransferred(0);

      const pathToFile = image;
      let filename = pathToFile.substring(pathToFile.lastIndexOf('-') + 1);
      const extension = filename.split('.').pop();
      const name = filename.split('.').slice(0, -1).join('.');
      filename = name + Date.now() + '.' + extension;

      const reference = storage().ref().child(`/userprofile/${filename}`);
      // path to existing file on filesystem

      // uploads file
      const task = reference.putFile(pathToFile);
      try {
        await task;

        const url = await reference.getDownloadURL();
        downloadableURI.push(url);
        if (index == imageUri.length - 1) {
          setUploading(false);
          Alert.alert(
            'Image uploaded!',
            'Your image has been uploaded to the Firebase Cloud Storage Successfully!',
          );
        }
      } catch (e) {
        console.log(e);
      }
    });
  };

whenever the function is called, then the array of images is uploaded to firebase storage,

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.