javascript - 如何检索 Firebase 数据库中存储的网址,然后填充图像标记的 src?

标签 javascript html firebase google-cloud-firestore

我遵循了有关如何创建 Firebase 数据库的精彩教程。 ( https://www.youtube.com/watch?v=kmTECF0JZyQ ) 然后我设法检索到此数据以显示在我的网页上。使用文本和数字很清楚,但是当我想在 HTML 页面上渲染图像时会发生什么?图片是从我的网站上提供的。

因此,我可以使用文本字段来存储图像的 URL,但是当创建标签时我如何在页面上显示它,但显然不是“src”来填充 URL。欢迎任何帮助。

const productList = document.querySelector('#productList');

function renderProducts(doc) {
    let li = document.createElement('li');
    let title = document.createElement('span');
    let price = document.createElement('span');
    let image = document.createElement('img');

    li.setAttribute('data-id', doc.id);
    title.textContent = doc.data().title;
    price.textContent = doc.data().price;
    image.textContent = doc.data().image;

    li.appendChild(title);
    li.appendChild(price);
    li.appendChild(image);

    productList.appendChild(li);
}

db.collection('Products').orderBy('title').get().then((snapshot) => {
    snapshot.docs.forEach(doc => {
        renderProducts(doc);
    })
})

最佳答案

img 元素没有 textContent 属性。我想你打算使用 img.src将 URL 加载到 img 中:

image.src = doc.data().image;

关于javascript - 如何检索 Firebase 数据库中存储的网址,然后填充图像标记的 src?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61174287/

相关文章:

javascript - 使用 raphael 在 svg 中创建图案元素

javascript - 自定义浏览器的默认表单错误

html - 类似airbnb的视频背景html/css

javascript - React Native 数据不会在第一次点击时推送到 firebase

ios - 将新请求上传到数据库时让 Firebase 发送电子邮件

javascript - 使内联 block 垂直对齐顶部?

javascript - HTML/Javascript 按钮不返回警报

javascript - 点击计数功能不起作用?

javascript - 检测浏览器本身是否支持将音频传递给输入标签

firebase - 我想在我的Flutter应用程序中从Firebase数据库中获取当前用户数据