gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 02:10:19 25 4
gpt4 key购买 nike

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

最佳答案

在你的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;

});

关于javascript - 如何从firebase实时数据库检索数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49609251/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com