无法将数据从firestore firebase提取到我的react组件

anauzrmj  于 2021-09-13  发布在  Java
关注(0)|答案(0)|浏览(135)

当试图从我的组件“customers.js”的表中检索名为“users”的集合中的数据时,我没有得到任何东西,控制台日志中也没有错误!在组件“customer.js”中,我将要获取的数据放在一个表中,如下所示。该表会显示出来,但它总是空的
customer.js

import PageTitle from "../../../../layouts/PageTitle";
import { Dropdown, Table } from "react-bootstrap";
import firebase from "../../../../../fire";
import { getFirestore } from "firebase/firestore";
import { collection, getDocs } from "firebase/firestore"; 

class Customers  extends React.Component {

   // const chack = (i) => (
   //    <div className={`custom-control custom-checkbox ml-2   `}>
   //       <input
   //          type="checkbox"
   //          className="custom-control-input "
   //          id={`checkAll${i}`}
   //          required=""
   //       />
   //       <label
   //          className="custom-control-label"
   //          htmlFor={`checkAll${i}`}
   //       ></label>
   //    </div>
   // );
   state = { 
      Users: [] 
   }
   constructor(props) {

      super(props);

   }
   componentDidMount() {

      firebase.firestore().collection("Users").get().then((querySnapshot) => {
        querySnapshot.forEach((doc) => {
      console.log(`${doc.id} => ${doc.data()}`);
  } );
} );  
      const drop = (
        <Dropdown>
           <Dropdown.Toggle variant="" className="table-dropdown icon-false">
              <svg width="24px" height="24px" viewBox="0 0 24 24" version="1.1">
                 <g
                    stroke="none"
                    stroke-width="1"   
                    fill="none"
                    fill-rule="evenodd"
                 >
                    <rect x="0" y="0" width="24" height="24"></rect>
                    <circle fill="#000000" cx="5" cy="12" r="2"></circle>
                    <circle fill="#000000" cx="12" cy="12" r="2"></circle>
                    <circle fill="#000000" cx="19" cy="12" r="2"></circle>
                 </g>
              </svg>
           </Dropdown.Toggle>
           <Dropdown.Menu>
              <Dropdown.Item href="widget-basic">Details</Dropdown.Item>

              <Dropdown.Item href="#" className="text-danger">
                 Delete
              </Dropdown.Item>
           </Dropdown.Menu>
        </Dropdown>
     );
      }   

   render(){

      return (
         <Fragment>
            <PageTitle activeMenu="Customers" motherMenu="Shop" />
            <div className="row">
               <div className="col-lg-12">
                  <div className="card">
                     <div className="card-body">
                        <div className="table-responsive">
                           <table className="table mb-0 table-striped">
                              <thead class="thead-dark">
                                 <tr>
                                    <th className="">
                                       <div className="custom-control custom-checkbox mx-2">
                                          <input
                                             type="checkbox"
                                             className="custom-control-input"
                                             id="checkAll"
                                          />
                                          <label
                                             className="custom-control-label"
                                             htmlFor="checkAll"
                                          ></label>
                                       </div>
                                    </th>
                                    <th>Avatar</th>
                                    <th>Email</th>
                                    <th>Firstname</th>
                                    <th>Lastname</th>
                                    <th>PhoneNumber</th>
                                    <th>Token</th>
                                    <th>Joined</th>

                                 </tr>
                              </thead>
                              <tbody>
                                 {this.state.Users.map(data => {

                                    return (
                                       <tr>
                                          <td>{data.avatar}</td>
                                          <td>{data.email}</td>
                                          <td>{data.firstname}</td>
                                          <td>{data.lastname}</td>
                                          <td>{data.phonenumber}</td>
                                          <td>{data.token}</td>
                                       </tr>

                                    );

                                 })}

                              </tbody>
                           </table>
                        </div>
                     </div>
                  </div>
               </div>
            </div>
         </Fragment>
      );
   };
};

export default Customers ;

这就是我配置thje fire.js的方式
fire.js

const firebaseConfig = {
    apiKey: "***********************",
    authDomain: "*******************",
    databaseURL: "**************************",
    projectId: "********************",
    storageBucket: "*******************",
    messagingSenderId: "***************",
    appId: "***********************",
    measurementId: "*************************"
  };
const fire = firebase.initializeApp(firebaseConfig);
const db = firebase.firestore().collection("Users").get().then((querySnapshot) => {
  querySnapshot.forEach((Users) => {
      console.log(`${d.id} => ${doc.data()}`);
  } );
} );
export default fire ;

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题