Firestore未捕获的TypeError:doc.data不是函数

当我想在node.js html中显示来自Firestore的数据时,在以下两个代码上出现错误: productName.textContent = doc.data()。name; productPrice.textContent = doc.data()。price;

Firestore未捕获的TypeError:doc.data不是函数

Firestore未捕获的TypeError:doc.data不是函数


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>
        );
    }
}
ether1984 回答:Firestore未捕获的TypeError:doc.data不是函数

似乎您正在传递数据,而不是对renderProduct的文档引用,因此您可以直接访问道具:

function renderProduct(data) {
   // ...
   article.setAttribute('id',data.id);
   productName.textContent = data.name;
   productPrice.textContent = data.price;
   // ...
,

您的代码将原始JavaScript数据传递到renderProduct

 products = doc.data();
 console.log(products);
 renderProduct(products);

它已经在调用doc.data()来获取该对象。但是随后函数继续对同一对象调用data()

productName.textContent = doc.data().name;
productPrice.textContent = doc.data().price;

只需删除对data()的多余呼叫。您可能还可能希望将参数从doc重命名为data,以使其更清楚期望值。

productName.textContent = doc.name;
productPrice.textContent = doc.price;
本文链接:https://www.f2er.com/3150981.html

大家都在问