React Native Firebase仅获取一个数据

时间:2018-03-13 13:17:35

标签: javascript reactjs firebase react-native firebase-realtime-database

如何只获取一个数据并将其写入标题?
我正在使用firebase和react-redux。

firebase结构我试着写" 组织":inovanka: firebase structure i try to write "**organization**": inovanka

行动档案代码:

import firebase from 'firebase';
import { Actions } from 'react-native-router-flux';
import { ORGANIZATION_NAME_DATA_SUCCESS } from './types';


 export const organizationName = () => {
    const { currentUser } = firebase.auth();
      return (dispatch) => {
        firebase.database().ref(`/organizations/${currentUser.uid}`)
          .on('value', snapshot => {
            dispatch({ type: ORGANIZATION_NAME_DATA_SUCCESS, payload: snapshot.val() });
        });
    };
  }

Reducer File:

import { ORGANIZATION_NAME_DATA_SUCCESS } from '../actions/types';

const INITIAL_STATE = {

};

export default (state = INITIAL_STATE, action) => {
  switch (action.type) {
    case ORGANIZATION_NAME_DATA_SUCCESS:
      console.log(action); // data retrieved as array
      return action.payload
    default:
      return state;
  }
};

组件:(我想把它写到这个)

    class HomePage extends Component {

      componentWillMount() { 
      }

      render() {
        return (
          <Container>
            <Header> 
              <Text> i would like to write it here </Text>
            </Header>
            <Content>

            </Content>
          </Container>
        );
      }
    }

    const mapStateToProps = ({ homepageResponse }) => {
      const organizationArray = _.map(homepageResponse, (val, uid) => {
        return { ...val, uid }; //
      });
      return { organizationArray };
    };

    export default connect(mapStateToProps, { organizationName })(HomePage);

2 个答案:

答案 0 :(得分:2)

改变这个:

firebase.database().ref(`/organizations/${currentUser.uid}`)
      .on('value', snapshot => {

到此:

firebase.database().ref(`/organizations/${currentUser.uid}`)
      .once('value', snapshot => {

使用once()只会读取一次数据,因此只能获取一个数据

答案 1 :(得分:0)

解决方案就在这里!

操作文件:

export const organizationName = () => {
    const { currentUser } = firebase.auth();
      return (dispatch) => {
        firebase.database().ref(`/organizations/${currentUser.uid}`)
        .once('value', snapshot => {
          _.mapValues(snapshot.val(), o => {
            console.log(o);
          dispatch({ type: ORGANIZATION_NAME_DATA_SUCCESS, payload: {organization: o.organization, fullname: o.fullname }});
          });
        });
    };
  }

Reducer文件

const INITIAL_STATE = {
  organization: '',
};

export default (state = INITIAL_STATE, action) => {
  switch (action.type) {
    case ORGANIZATION_NAME_DATA_SUCCESS:
      console.log(action);
      return {...state, organization:action.payload.organization };
    default:
      return state;
  }
};

组件文件MapToStateProps和componentWillMount

 const mapStateToProps = state =>  {
    const { organization, fullname  } = state.homepageResponse;
    console.log("burada" + organization);
       return { organization, fullname };
};




 componentWillMount(){
    this.props.organizationName();
  }

*最后一步标题*

render() {
    return (
      <Container>
        <Header>
          <Text>  { this.props.organization } </Text>
        </Header>
      </Container>
}

谢谢大家