0

All seems to works perfectly except i cant push my value to my array inside the promise all function ?? What am i doing wrong here ?

var reponses = [];

Object.values(thequestion.answers).forEach(item => {
  var mars = item.mars;

  var getFullName = FirebaseRef.child('/users/' + mars + '/fullName').once('value');
  var getAvatar = FirebaseRef.child('/users/' + mars + '/avatar').once('value');
  var getDescription = FirebaseRef.child('/users/' + mars + '/description').once('value');

  // console.log(queries);
  Promise.all([getFullName, getAvatar, getDescription]).then(answer => {
    var oneanswer = {
      answer: item.answer,
    };

    reponses.push(oneanswer);
  });
  console.log('oneanswer', reponses);
});

3
  • Have you tried debugger or adding a console.log() directly above the .push() to make sure your promise is entering the .then Commented Nov 17, 2019 at 19:14
  • yes it's entering. Commented Nov 17, 2019 at 19:16
  • When you call console log, responses hasn't been populated yet. If you call it just after push it should include the new value. Is this the problem? Commented Nov 17, 2019 at 19:35

1 Answer 1

1

Pay attention that your console.log is outside of the async flow.

Therefore, you need to move it inside the "then" of the Promise.

var reponses = [];

Object.values(thequestion.answers).forEach(item => {
  var mars = item.mars;

  var getFullName = FirebaseRef.child('/users/' + mars + '/fullName').once('value');
  var getAvatar = FirebaseRef.child('/users/' + mars + '/avatar').once('value');
  var getDescription = FirebaseRef.child('/users/' + mars + '/description').once('value');

  // console.log(queries);
  Promise.all([getFullName, getAvatar, getDescription]).then(answer => {
    var oneanswer = {
      answer: item.answer,
    };

    reponses.push(oneanswer);
    console.log('oneanswer', reponses);
    // -------^
  });
});
Sign up to request clarification or add additional context in comments.

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.