Firestore未捕获(承诺)TypeError:无法读取null的属性'appendChild'

时间:2019-11-11 17:19:10

标签: javascript node.js google-cloud-firestore

当我想在node.js中显示来自Firestore的数据时,出现以下代码错误: productsContainer.appendChild(article);

出现此错误: Firestore未捕获(承诺)TypeError:无法读取null的属性'appendChild'

const myProducts = db.collection('products').doc('fruit');
const productsContainer = document.querySelector('#groceries');

 function renderProduct(data) {
   const docFrag = document.createDocumentFragment();
   let article = document.createElement('article');
   let productName = document.createElement('h4');
   let productPrice = document.createElement('p');

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

   docFrag.appendChild(productName);
   docFrag.appendChild(productPrice);

   article.appendChild(docFrag);
   productsContainer.appendChild(article);
 }

myProducts.get().then(function(documentSnapshot) {
  if (documentSnapshot.exists) {
    var products = documentSnapshot.data();
    console.log(products);
    renderProduct(products);
  }
});

class contact 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>
      );
  }
}

console firestore

0 个答案:

没有答案