Firebase stopping the iteration of foreach

Asked

Viewed 54 times

0

Goal:

I’m trying to make an array of objects containing 3 information: id, title and imageUri. But when taking the value of imageUri firebase(a download URL), the foreach hangs.

Error:[Unhandled Promise rejection: Typeerror: JSON.stringify cannot serialize Cyclic Structures.]

observing: When I take back the firebase part(imageUri: firebase...), works as expected

the function:

    processData = ( data ) => {
        console.log('---------data recebida na processData(Main.js:70)--------\n', data)
        var localProcessedData = [];

        Object.entries(data).forEach( ([key, value]) => {
          var event = {
            id: key,
            title: Object.getOwnPropertyDescriptor(value, "eventTitle").value,
            imageUri: firebase.storage().ref('events/active/' + key + '/image').getDownloadURL()
          }
          localProcessedData.push(event);
        })

        this.setState({
          processedData: localProcessedData,
          eventsDataIsLoaded: true,
        })
      }

the type of data the function takes as a parameter:

     Object {
      "-M-I83aV9t1fezOsBn17": Object {
        "active": true,
        "created": "2020-02-05T02:18:30.772Z",
        "description": "Olimpiadas Inter Atletica",
        "eventTitle": "oia",
        "location": "Uberlandia",
        "owner": "p87xn6x8DZTwb6qyTadhkk3UxJV2",
        "price": "130",
        "startDate": "15",
        "time": "14",
        "updated": "2020-02-05T02:18:30.772Z",
      },
      "-M-KlUH-zQhnIhb6wMH8": Object {
        "active": true,
        "created": "2020-02-05T14:34:20.399Z",
        "description": "Cia 2020",
        "eventTitle": "Cia",
        "location": "Uberlandia",
        "owner": "p87xn6x8DZTwb6qyTadhkk3UxJV2",
        "price": "130340",
        "startDate": "15",
        "time": "14",
        "updated": "2020-02-05T14:34:20.399Z",
      }
    }

My goal is to format this data and turn it into an array like this:

    Array [
      Object {
        "id": "-M-I83aV9t1fezOsBn17",
        "title": "oia",
        "imageUri": "url da imagem"
      },
      Object {
        "id": "-M-KlUH-zQhnIhb6wMH8",
        "title": "Cia",
        "imageUri": "url da imagem"
      }
    ]
  • Give it here https://stackoverflow.com/questions/42266462/react-native-json-stringify-cannot-serialize-cyclical-structures

  • @Leandrade I had read it earlier but I didn’t understand it very well

  • the function getDownloadUrl don’t return a Promise? or I’m wrong?

  • @Leandrosimões yes

  • Then, in that case you would have to perform this function, get the result of it, through the .then and only then, with the result in hand, continues the function and creates the object event. The way you’re doing there, the estate imageUri of the object event is not the url itself, but a Promise not "solved", I do not know if I could understand?

1 answer

0


I decided to make the function stay async:

Object.entries(data).forEach( async ([key, value]) => {
  var event = {
     id: key,
     title: Object.getOwnPropertyDescriptor(value, "eventTitle").value,
     imageUri: await firebase.storage().ref('events/active/' + key + '/image').getDownloadURL()
  }
  localProcessedData.push(event);
})```

Browser other questions tagged

You are not signed in. Login or sign up in order to post.