Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

144
Views
React Synchronization problem with Firebase

import React from 'react';
import { withStyles } from '@material-ui/core/styles';
import {Card, CardContent, CardActions, Typography, IconButton} from '@material-ui/core';

import firebase from './firebase';

class LikeMusicList extends React.Component{
 
  render () {
    let db = firebase.firestore();
    let snapshot = db.collection("likes");
    let list = []

    snapshot.get().then(function(querySnapshot) {
      if (querySnapshot) {
        querySnapshot.forEach(function(item){
          fetch(`https://itunes.apple.com/lookup?id=${item.id}&entity=album`).then(r => r.json()).then(r => {
            list.push(r.results[0]);
            console.log(list);
          }).catch(e => console.log(e));
        })
      }
      else {
          console.log("No such document!");
      }
    }).catch(function(error) {
        console.log("Error getting document:", error);
    });
    //console.log(list);

    return (
      <div>
          {console.log(list)}
          {list.map(item => {
            <Card key={item.collectionId} className={classes.card}>
              <CardContent>
                  <Typography variant="subtitle1"> {item.artistName}</Typography>
                  <Typography variant="subtitle2"> {item.collectionCensoredName}</Typography>
              </CardContent>
              <img src = {item.collectionViewUrl} alt = {item.collectionName}></img>
            </Card>
          })}
      </div>
    );
  };
}

export default LikeMusicList

I want to do rendering these data with fetching the albums in Itunes. But because of synchronization problem, in return the varialbe 'list' is empty. I think that the code 'snapshot.get().~' is executed before return, but result was reverse of that. How do I solve this problem?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You solve the problem by using state and moving the firestore function to react lifecycle function called componentDidMount:

import React from 'react';
import { withStyles } from '@material-ui/core/styles';
import {Card, CardContent, CardActions, Typography, IconButton} from '@material-ui/core';

import firebase from './firebase';

class LikeMusicList extends React.Component{
  
  state = {
    list: []
  }
  
  componentDidMount() {
    let db = firebase.firestore();
    let snapshot = db.collection("likes");

    snapshot.get().then(function(querySnapshot) {
      if (querySnapshot) {
        querySnapshot.forEach(function(item){
          fetch(`https://itunes.apple.com/lookup?id=${item.id}&entity=album`).then(r => r.json()).then(r => {
            this.setState({
              list: r.results[0]
            })
            console.log(r.results[0]);
          }).catch(e => console.log(e));
        })
      }
      else {
          console.log("No such document!");
      }
    }).catch(function(error) {
        console.log("Error getting document:", error);
    });

  }
 
  render () {

    return (
      <div>
          {this.state.list.map(item => {
            <Card key={item.collectionId} className={classes.card}>
              <CardContent>
                  <Typography variant="subtitle1"> {item.artistName}</Typography>
                  <Typography variant="subtitle2"> {item.collectionCensoredName}</Typography>
              </CardContent>
              <img src = {item.collectionViewUrl} alt = {item.collectionName}></img>
            </Card>
          })}
      </div>
    );
  };
}

export default LikeMusicList
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!