我该如何访问firebase嵌套对象

时间:2017-07-23 22:34:13

标签: javascript reactjs firebase firebase-realtime-database

Hey , I want to access appliers node's data , anyone can tell me what is the best practice for fetching data from root level to targeted node as i discussed appliers node

Here is Code present that i am tried

import React from "react"
import * as firebase from 'firebase';
import NavBarSignOut from '../navBar/navBarSignOut';
import {Link,browserHistory} from 'react-router';




class Candidiate extends React.Component{
    constructor(props){
        super(props);
        this.state = {
          myProfile : [],
          jobKeys : []
        }
    }
    componentDidMount(){
        const refRoot = firebase.database().ref();
        const rootRef = refRoot.child('jobs');
        rootRef.on('value',snapshot=>{
            var userObj = snapshot.val();
               console.log(userObj);            
             let vm = this;
             let obj = [];
             let keys = [];
                Object.keys(userObj).forEach(function (key) {
                 obj = userObj[key]       
                 keys.push(key);
            vm.state.myProfile.push(obj)
            vm.setState({ myProfile: vm.state.myProfile,  
                            jobKeys : keys
                          })
                });    
     })
              const apply =  rootRef.child(this.state.jobKeys+'/appliers').on('value',snap=>{
                    var apply = snap.val();
                    console.log(apply);
                    let a = [];
                    Object.keys(apply).forEach(function (key){
                        a = apply[key];
                        console.log(a);
                        this.setState({myProfile : this.state.a})
                    })
                })      
    }

    render(){
        return  firebase.auth().currentUser ? (
            <div>
                <NavBarSignOut />
               <center> <h1>Profile</h1> </center>
                 {this.state.myProfile.map((data , index) => (
                         <div key={index} className="container">

    <table className="table">
        <thead >
      <tr>
        <th>Name</th>
        <th>Education</th>
        <th>Skills</th>
        <th>Email</th>
        <th>GPA</th>   
        <th>Experience</th>                
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>{data.sName}</td>
        <td>{data.education}</td>
        <td>{data.skills}</td>
        <td>{data.sEmail}</td>
        <td>{data.gpa}</td>
        <td>{data.experience}</td>        
      </tr>
    </tbody>
  </table>

</div>      ))}
 <Link to="student" className="btn btn-primary">
    Go Back student Panel
  </Link>
            </div>
        ) : ( <div>{browserHistory.push('login')}</div> )
    }
}
export default Candidiate;

JSON DATA:

{
  "jobs" : {
    "-KpdYaI_E5Vp39bkeFww" : {
      "appliers" : {
        "JfUXMVNDrnVcnOkn8tZ1LfUFf2P2" : {
          "education" : "cma",
          "experience" : "not",
          "gpa" : "4",
          "sEmail" : "talha@gmail.com",
          "sId" : "JfUXMVNDrnVcnOkn8tZ1LfUFf2P2",
          "sName" : "talha",
          "skills" : "finance"
        }
      },
      "companyName" : "panacloud",
      "education" : "",
      "email" : "pana@gmail.com",
      "jobTitle" : "",
      "skills" : "",
      "uid" : "CcyYpW5c3GOcU3ltBIpSSg9cnd33"
    },
    "-KpeKF86tqYdosIOJP8M" : {
      "appliers" : {
        "JfUXMVNDrnVcnOkn8tZ1LfUFf2P2" : {
          "education" : "cma",
          "experience" : "not",
          "gpa" : "4",
          "sEmail" : "talha@gmail.com",
          "sId" : "JfUXMVNDrnVcnOkn8tZ1LfUFf2P2",
          "sName" : "talha",
          "skills" : "finance"
        }
      },
      "companyName" : "ibm",
      "education" : "bcom",
      "email" : "ibm@gmail.com",
      "jobTitle" : "data entry ",
      "skills" : "english",
      "uid" : "TgrXg6Fx0WZFhmNHRY0nxsLxkbL2"
    },
    "-Kpeew-PmNnqEB6f4Q_g" : {
      "companyName" : "ibm",
      "education" : "bscs",
      "email" : "ibm@gmail.com",
      "jobTitle" : "front end",
      "skills" : "html , css ,js",
      "uid" : "TgrXg6Fx0WZFhmNHRY0nxsLxkbL2"
    }
  },
  "update Profile" : {
    "JfUXMVNDrnVcnOkn8tZ1LfUFf2P2" : {
      "education" : "cma",
      "experience" : "not",
      "gpa" : "4",
      "sEmail" : "talha@gmail.com",
      "sId" : "JfUXMVNDrnVcnOkn8tZ1LfUFf2P2",
      "sName" : "talha",
      "skills" : "finance"
    },
    "vGHLdbDPF1YcvFOwzULmc0uIQUF3" : {
      "education" : "fcma",
      "experience" : "no",
      "gpa" : "4",
      "sEmail" : "student@gmail.com",
      "sId" : "vGHLdbDPF1YcvFOwzULmc0uIQUF3",
      "sName" : "student",
      "skills" : "taxation"
    }
  },
  "user" : {
    "CcyYpW5c3GOcU3ltBIpSSg9cnd33" : {
      "typeOfUser" : "company",
      "uEmail" : "pana@gmail.com",
      "uName" : "panacloud",
      "uPassword" : "12345678"
    },
    "JfUXMVNDrnVcnOkn8tZ1LfUFf2P2" : {
      "typeOfUser" : "student",
      "uEmail" : "talha@gmail.com",
      "uName" : "talha",
      "uPassword" : "12345678"
    },
    "TgrXg6Fx0WZFhmNHRY0nxsLxkbL2" : {
      "typeOfUser" : "company",
      "uEmail" : "ibm@gmail.com",
      "uName" : "ibm",
      "uPassword" : "12345678"
    },
    "vGHLdbDPF1YcvFOwzULmc0uIQUF3" : {
      "typeOfUser" : "student",
      "uEmail" : "student@gmail.com",
      "uName" : "student",
      "uPassword" : "12345678"
    }
  }
}

0 个答案:

没有答案