【问题标题】:How to retrieve data from firebase realtime database?如何从 Firebase 实时数据库中检索数据?
【发布时间】:2018-09-11 13:13:08
【问题描述】:

我正在检索 firebase 数据库,并希望在网页上显示数据,如类别、描述等。我怎样才能让这件事发挥作用?我想在类别、描述等文本旁边显示值。

JS

var myParam = location.search.split('itemKey=')[1];
firebase.database().ref('/Sell_Products/' + myParam).once('value').then(function (snapshot) {
    var name = snapshot.child('name').val();
    alert(name);
    var image = snapshot.child('image').val();
    var category = snapshot.child('category').val();
    var description = snapshot.child('description').val();
    var price = snapshot.child('price').val();
    var user = firebase.auth().currentUser.uid;
    var databaseRef = firebase.database().ref("auction/");
    databaseRef.set({
        Image: image,
        category: category,
        description: description,
        price: price,
        uid: user
    });
    document.querySelector('#image').src = image;
});

HTML

<div id="image1">
    <div id="box">
        <img id="image" />
    </div>
</div>
<div>
    <div id="content">
        <p id="category">Category: </p>
        </br>
        <p id="name">Name:</p>
        </br>
        <p id="price">Price:</p>
        </br>
        <p id="decription">Description:</p>
        </br>
    </div>
</div>
<div id="btn">
    <button class="button">BID</button>
</div>

My web page

【问题讨论】:

标签: javascript html firebase firebase-realtime-database


【解决方案1】:

在你的html中添加:

<p id="category">Category: <span id="categorytext">sample text</span> </p>

然后在js文件中:

 firebase.database().ref('/Sell_Products/'+myParam).once('value').then(function(snapshot)
{var name=snapshot.child('name').val();
 alert(name);
 var image=snapshot.child('image').val();
 var category=snapshot.child('category').val();
 var description=snapshot.child('description').val();
 var price=snapshot.child('price').val();
 document.getElementById("categorytext").innerHTML = category;

});

【讨论】:

  • 我按照您的建议进行了研究,但不幸的是它没有成功。还有其他方法吗?
  • 我已经使用了 我想在其中显示我正在检索的值,但那里没有显示任何内容。我为每个文本区域和脚本提供了 id我正在写标签 document.getElementById("categorytext").innerHTML = category; categorytext 是文本区域的id
  • 检查这个:stackoverflow.com/a/21711717/7015400@AbhishekKumar
  • 是的,我已经记录了我的投票。 @Peter Haddad 你能看到这个问题吗stackoverflow.com/q/49638806/7633292 please.Thanks
  • 关于链接,答案是正确的,它会检查用户是否登录
猜你喜欢
  • 1970-01-01
  • 2019-04-16
  • 2020-12-09
  • 1970-01-01
  • 2019-09-14
  • 2021-02-06
  • 2021-08-22
  • 2022-01-08
相关资源
最近更新 更多