简体   繁体   English

React redux + 从 Firebase 获取数据

[英]React redux + fetch data from Firebase

While I am trying to fetch user data from firebase in my user reducer like this:当我试图在我的 user reducer 中从 firebase 获取用户数据时,如下所示:

const initialState = {
    user : {
        firstname : "",
        lastname : "",
        todoList : [],
        addressBook : []
    }
};

const usersReducer = async (state = initialState, action) => {
    const app = initializeApp(firebaseConfig);
    const db = getFirestore(app);

    switch(action.type) {

        case FETCH_USER_DATA:
            const docRef = doc(db, "users", action.payload.userUid);
            const docSnap = await getDoc(docRef);
            if (docSnap.exists()) {
              console.log("Document data:", docSnap.data());
            } else {
              console.log("No such document!");
            }
            return {
                user: {firstname: docSnap.data().firstname, lastname: docSnap.data().lastname, todoList: [], addressBook: []}
            }
        
        default:
            return state;
    }
}

When I get my user state in my components I receive this object:当我在我的组件中获取我的用户状态时,我收到这个对象:

{"_A": null, "_x": 0, "_y": 1, "_z": {"user": {"addressBook": [Array], "firstname": "XXX", "lastname": "XXX", "todoList": [Array]}}} {“_A”:空,“_x”:0,“_y”:1,“_z”:{“用户”:{“地址簿”:[数组],“名字”:“XXX”,“姓氏”:“ XXX", "todoList": [数组]}}}

I dont' understand why I get this attribute in my object: "_A": null, "_x": 0, "_y": 1, "_z":我不明白为什么我在我的对象中得到这个属性: "_A": null, "_x": 0, "_y": 1, "_z":

It works for me:这个对我有用:

const ref = db
  .collection('users')
  .doc(action.payload.userUid)

const doc = await ref.get();

if (!doc.exists) {
  console.log('Not exists');
} else {
  console.log('data', doc.data())
}

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

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