javascript - Firestore Uncaught TypeError:doc.data 不是函数
问题描述
当我想在 node.js html 中显示来自 firestore 的数据时,这两个代码出现错误:productName.textContent = doc.data().name; 和 productPrice.textContent = doc.data().price;
const myProducts = db.collection('products');
const productsContainer = document.querySelector('#groceries');
function renderProduct(doc) {
const docFrag = document.createDocumentFragment();
let article = document.createElement('article');
let productName = document.createElement('h4');
let productPrice = document.createElement('p');
article.setAttribute('id', doc.id);
productName.textContent = doc.data().name;
productPrice.textContent = doc.data().price;
docFrag.appendChild(productName);
docFrag.appendChild(productPrice);
article.appendChild(docFrag);
productsContainer.appendChild(article);
}
myProducts.onSnapshot(products => {
products.forEach(doc => {
products = doc.data();
console.log(products);
renderProduct(products);
});
});
class shop extends React.Component {
render() {
return (
<div className="app-wrapper">
<ContainerHeader match={this.props.match} title={<IntlMessages id="appModule.contactSupport"/>}/>
<div className="d-flex justify-content-center">
<div id="groceries"></div>
</div>
</div>
);
}
}
解决方案
似乎您正在传递数据,而不是对 的文档引用renderProduct
,因此您只需直接访问道具:
function renderProduct(data) {
// ...
article.setAttribute('id', data.id);
productName.textContent = data.name;
productPrice.textContent = data.price;
// ...
推荐阅读
- android - 无法更改 ListAdapter 的 currentList 中的项目数据
- javascript - 如何在包含坐标对的数组上使用 find 方法?
- django - django应用的AWS部署
- android - 授予应用绑定服务的权限
- mysql - 如何在mysql表中找到连续的间隔
- c++ - 从指向结构数组的指针中提取元素
- javascript - 使用地图功能(javascript)将元素添加到列表
- html - 有没有办法使用 CSS 为我拥有的这个水平菜单创建一个下拉菜单?我试图在“文章”部分创建下拉菜单
- java - 打开堆转储时,Android Studio 中的 Profiler 为空白
- c# - C# Web API - 当我没有传递所有查询字符串参数时收到错误消息