【问题标题】:React Native can't read Database FirebaseReact Native 无法读取数据库 Firebase
【发布时间】:2020-03-21 00:49:04
【问题描述】:

我无法读取要插入“文本”的数据库。我正在使用 Redux

json

DiasSemana
 |
 |__Quarta: "Test Program"
 |
 |__Quinta: "nothing"
 |
 |

Class EstudoDia.js 我使用了 mapStateToProps,连接和导入动作。

import React, { Component } from "react";
import { View, Text } from "react-native";
import { Actions } from "react-native-router-flux";
import { connect } from "react-redux";
import firebase from "firebase";
import { estudoDoDia } from "../actions/AutenticacaoActions";

export class estudoDia extends Component {
  componentWillMount() {
    //I've tried with 'this.props.quarta' and it didn't work
    props.estudoDia(props.quarta);
  }

  render() {
    return (
      <View>
        <Text>{props.quarta}</Text>
      </View>
    );
  }
}

const mapStateToProps = state => ({
  quarta: state.AutenticacaoReducer.quarta
});
export default connect(mapStateToProps, { estudoDoDia })(estudoDia);

types.js

export const MATERIA_DO_DIA = 'materia_do_dia';

ActionReducer.js。我导入类型并返回有效负载

import { MATERIA_DO_DIA } from "../actions/types";

const INITIAL_STATE = {
  quarta: "T.I Program"
};

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

    default:
      return state;
  }
};

AutenticacaoActions.js。使用快照

import firebase from "firebase";
import { Actions } from "react-native-router-flux";
import { MATERIA_DO_DIA } from "./types";

export const estudoDoDia = quarta => {
  return dispatch => {
    firebase.database
      .ref("/DiasSemana/Quarta")
      .once("value")
      .then(snapshot => {
        quarta = snapshot.val();
        dispatch({
          type: MATERIA_DO_DIA,
          payload: snapshot.val
        });
      });
  };
};

链接中有图片 enter image description here

如果你能帮助我,谢谢你

【问题讨论】:

  • 这里涉及的部分很多,因此很难提供帮助。帮助调试的最简单的问题。如果您在quarta = snapshot.val(); 上设置断点并在调试器中运行,它会命中该断点吗?如果是这样,quarta 是否具有您期望的价值?
  • 你的数据库初始化在哪里?您刚刚从“firebase”导入了 import firebase;无需初始化。
  • 另外,尝试更改一次为 on
  • @FrankvanPuffelen 所以这是最大的问题,它甚至没有达到第四个 = snapshot.val () 的那个点,但我设法修改了这个函数
  • @dhanushkac 如果您已将此徽标放在第一个屏幕上,以便在您启动应用程序时立即对其进行初始化,并正确执行导入,您将找出哪个错误是或不是使用快照方法。 on 而不是 once 是一个很好的尝试。

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


【解决方案1】:

感谢大家的帮助,以防我发现问题出在 AuthenticationActions.js 中的 estudoDoDia 方法中,我进行了此更改并且成功了

export const estudoDoDia = (quarta) => {    
    return dispatch => {

        firebase.database().ref('DiasSemana/Quarta').once('value', function (snapshot) {
            console.log(snapshot.val())
            quarta = snapshot.toJSON();

 dispatch(
{
    type: MATERIA_DO_DIA,
    payload: quarta
})  
})
 }
}

一旦我让它调用一个将 snapshop 作为参数传递的函数,它就可以在里面工作。 谢谢大家的帮助,谢谢大家

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-07
    • 1970-01-01
    • 2019-05-20
    • 1970-01-01
    • 2022-01-23
    相关资源
    最近更新 更多