如何从firebase实时数据库中检索数据?

时间:2018-04-02 10:07:06

标签: javascript html firebase firebase-realtime-database

我正在检索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

1 个答案:

答案 0 :(得分: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;

});