简体   繁体   English

如何使用 javascript 从 firebase 中的键中获取值

[英]How do I fetch the value from a key in firebase using javascript

看到我想在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它只显示键值而不显示其他值我想查看键值以及键值的值请大家帮忙

这是我的 javascript 的输出

You are using forEach() and DataSnapshot of /orders node where each child node seems to be a category and not the image itself.您正在使用/orders节点的forEach()DataSnapshot ,其中每个子节点似乎是一个类别而不是图像本身。 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)
      })
    })
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM