简体   繁体   English

从 firebase v9 检索到的数据未定义

[英]Retrieved data from firebase v9 is undefined

When retrieving data and console.log it, the data shows perfectly, but when trying to dispatch the action with the argument as a data it turns out to be undefined .当检索数据并对其进行console.log时,数据显示完美,但是当尝试使用参数作为数据分派操作时,结果是undefined

I tried to use await before dispatch the action, but it didn't change anything.我尝试在dispatch操作之前使用 await,但它没有改变任何东西。 Why does it happen?为什么会这样?

数据未定义

actions.js动作.js

import * as types from './actionTypes'
import { db } from '../firebase';
import { collection, getDocs } from "firebase/firestore";

const getFeedbacksStart = () => ({
    type: types.GET_FEEDBACKS_START,
});

const getFeedbacksSussess = (feedbacks) => ({
    type: types.GET_FEEDBACKS_SUCCESS,
    payload: feedbacks
});

const getFeedbacksFail = () => ({
    type: types.GET_FEEDBACKS_FAIL,
});


export const getFeedbacks = () => {
    return async function (dispatch) {
        dispatch(getFeedbacksStart());

        try {
            const querySnapshot = await getDocs(collection(db, "feedbacks"));
            querySnapshot.forEach((doc) => {
                console.log(doc.id, " => ", doc.data())
            });

            const feedbacks = querySnapshot.forEach((doc) => doc.data());

            dispatch(getFeedbacksSussess(feedbacks))


        } catch (error) {
            dispatch(getFeedbacksFail(error))
        }
    }
}

actionTypes.js actionTypes.js

export const GET_FEEDBACKS_START = 'GET_FEEDBACKS_START';
export const GET_FEEDBACKS_SUCCESS = 'GET_FEEDBACKS_SUCCESS';
export const GET_FEEDBACKS_FAIL = 'GET_FEEDBACKS_FAIL';

reducer.js reducer.js

import * as types from './actionTypes'

const initialState = {
    feedbacks: {},
    loading: false,
    error: null,
};

const feedbackReducer = (state = initialState, action) => {
    switch (action.type) {
        case types.GET_FEEDBACKS_START:
            return {
                ...state,
                loading: true
            }
        case types.GET_FEEDBACKS_SUCCESS:
            return {
                ...state,
                loading: false,
                feedbacks: action.payload,
            }
        case types.GET_FEEDBACKS_FAIL:
            return {
                ...state,
                loading: false,
                error: action.payload,
            }
        default:
            return state;
    }

}

export default feedbackReducer;

root-reducer.js root-reducer.js

import { combineReducers } from "redux";

import feedbackReducer from "./reducer";

const rootReducer = combineReducers({
    data: feedbackReducer, 
});

export default rootReducer;

store.js商店.js

import { configureStore } from '@reduxjs/toolkit';
import thunk from 'redux-thunk';
import logger from 'redux-logger';

import rootReducer from './root-reducer';
  
const store = configureStore({
    reducer: rootReducer,
    middleware: [thunk, logger],
});

export default store;

ListRecord.js where I dispatch the action ListRecord.js我在其中发送操作

import React, { useEffect, useState, useContext } from "react";
import { useSelector, useDispatch } from 'react-redux';
import { getFeedbacks } from "../redux/actions";


const ListRecord = () => {
    const [data, setData] = useState({});
    console.log("data", data);

    const state = useSelector(state => state.data);
    console.log("state =>", state);

    let dispatch = useDispatch();

    useEffect(() => {
        dispatch(getFeedbacks());
    }, [])



    return (
        <>

        </>
    );
};

export default ListRecord;

I figured out what I was doing wrong.我弄清楚我做错了什么。 I tried to retrieve the data in the wrong way.我试图以错误的方式检索数据。 I was trying to use forEach method on a collection.我试图在集合上使用forEach方法。 Firstly, it needed to refer to the docs inside a db -> querySnapshot.docs and then you can use .map() method and loop through the whole collection you have inside your database.首先,它需要引用 db -> querySnapshot.docs中的文档,然后您可以使用.map()方法并遍历数据库中的整个集合。

The example of how to do it right with firebase v9 is HERE如何正确使用 firebase v9 的示例在这里

Here is a working code:)这是一个工作代码:)

In actions.jsactions.js

export const getFeedbacks = () => {
    return function (dispatch) {
        dispatch(getFeedbacksStart())

        const getData = async () => {
            try {
                const querySnapshot = await getDocs(collection(db, "feedbacks"));
                const feedbacks = querySnapshot.docs.map((doc) => ({
                    ...doc.data(),
                    id: doc.id
                }))
                dispatch(getFeedbacksSussess(feedbacks));

            } catch (error) {
                dispatch(getFeedbacksFail(error))
            }

        }
        getData();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM