1

See the image i want to fetch this value in javascript

 var dbImages = firebase.database().ref("images");

 dbImages.on("value", function(images){

    if(images.exists()){
        var imageshtml = ""; 
        images.forEach(function(fetchImages){

             console.log("key: " + fetchImages.key);
             console.log("title: " + fetchImages.title);
             console.log("url: " + fetchImages.url);

        });
     }

});

Its only showing key value not other value i want to view key value and also the value of key value please help guys

This is the output of my javascript

2
  • Hey Dharmaraj you also use console to view Commented Apr 2, 2022 at 5:58
  • Can you try using the code in my answer with nested forEach? Commented Apr 2, 2022 at 6:13

1 Answer 1

1

You are using forEach() and DataSnapshot of /orders node where each child node seems to be a category and not the image itself. You'll have to loop over each image of the categories to list them:

firebase.database().ref("images").once("value").then((imagesCategories) => {
  if (imagesCategories.exists()) {
    var imageshtml = "";

    // For each category
    imagesCategories.forEach(function(categoryImages) {
      // For each category image
      categoryImages.forEach(function(image) {
        // Append data to imageshtml from here
        console.log(image.val().url)
        console.log(image.val().title)
      })
    })
  }
});
Sign up to request clarification or add additional context in comments.

4 Comments

Uncaught TypeError: firebase.database(...).ref(...).get is not a function
@AmitYadav sorry its .ref("images").once("value") and not get()
Uncaught SyntaxError: await is only valid in async functions and the top level bodies of modules
@AmitYadav right so can you use an async function or use promise chaining as in my updated answer... ? Also checkout async functions on MDN

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.