0

I have many articles in my firebase realtime database and I have named their node in a series of numbers.

I want to retrieve them using a for-loop. Before that I check how many articles are available and then run the FOR loop accordingly. But when I receive number of articles available the program stops and the for loop won't run.

Here is my code:

let numOfArticlesAvailable;

const numOfArticlesRef = firebase.database().ref('/articles/available-count');
numOfArticlesRef.on('value', function (snapshot) {
    console.log('Articles Available: ' + snapshot.val());
    numOfArticlesAvailable = snapshot.val();
    console.log(numOfArticlesAvailable);
})

for (let index = numOfArticlesAvailable; index >= 1; index--) {
    const articlesRef = firebase.database().ref(`/articles/${index}`);
    console.log(articlesRef);
    articlesRef.on('value', function (snapshot) {
        const title = snapshot.val().title;
        const content = snapshot.val().content;
        const timestamp = snapshot.val().timestamp;

        console.log(`${title} has contents "${content}" and timestamp ${timestamp}`)
    })
}

The code log the correct value of articles available but the for-loop just won't start.

3
  • You need to run the loop under numOfArticlesRef.on('value', function (snapshot) {}) callback Commented May 3, 2020 at 11:33
  • @AshishRanjan my code is going to have more further steps. Am I supposed to enclose all of it in that callback then? Commented May 3, 2020 at 11:34
  • Yeah, maybe enclose the logic under different functions and call those functions from under the callback Commented May 3, 2020 at 11:35

1 Answer 1

1

Your for loop is not starting because numOfArticlesRef.on('value', function (snapshot) { is async function. This means that in the moment you for loop starts, value of numOfArticlesRef is either undefined or 0. or something like that. Try to do this:

let numOfArticlesAvailable;

const numOfArticlesRef = firebase.database().ref('/articles/available-count');
numOfArticlesRef.on('value', function (snapshot) {
    console.log('Articles Available: ' + snapshot.val());
    numOfArticlesAvailable = snapshot.val();
    console.log(numOfArticlesAvailable);
for (let index = numOfArticlesAvailable; index >= 1; index--) {
    const articlesRef = firebase.database().ref(`/articles/${index}`);
    console.log(articlesRef);
    articlesRef.on('value', function (snapshot) {
        const title = snapshot.val().title;
        const content = snapshot.val().content;
        const timestamp = snapshot.val().timestamp;

        console.log(`${title} has contents "${content}" and timestamp ${timestamp}`)
    })
}

})
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.