0

I'm trying to set an object on Firebase database realtime by looping a list. For example, I have list of Objects. I want to run over that list and each loop save on database realtime the object. This is the code:

  if(feedVideosList.length > 0){
       feedVideosList.forEach(video =>{
         return admin.database().ref('/userFeed/${userUID}/').set(video);
     }).catch(error =>{
      console.log("Error user feed:  ",error);
    });
  }else{
    console.log("Problem with the videos list:  ",error);
  }

I'm pretty sure that this code is wrong and thats not the way I should write it. Like i said, what I'm trying to do is to run over a list and save each time the data on spasific node. The question is how should i do that, whats the right way to do that?

1 Answer 1

2

If you're running this code in Cloud Functions, my best guess is that you want the Cloud Function to terminate after all writes to the database are completed. You can use Promise.all() for that:

  if(feedVideosList.length > 0){
    let promises = []
     feedVideosList.forEach(video =>{
       promises.push(admin.database().ref('/userFeed/${userUID}/').set(video));
     })
     return Promise.all(promises).catch(error =>{
      console.log("Error user feed:  ",error);
    });
  }else{
    console.log("Problem with the videos list:  ",error);
  }
Sign up to request clarification or add additional context in comments.

1 Comment

Exactly what i needed

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.