【问题标题】:Redux store.getState() returning unexpected long object, possible promise issueRedux store.getState() 返回意外的长对象,可能的承诺问题
【发布时间】:2021-08-05 02:42:16
【问题描述】:

我正在尝试为电子商务风格的应用设置我的 redux 商店。我们的数据库是 Firebase,我试图在 redux 商店中模仿 Firebase 的数据结构。我首先使用用户“喜欢”数据对其进行测试,该数据是结构化的并从 Firebase 作为对象返回,如下所示:

Object {
  "author": "Stephen Turns",
  "condition": "Run Over By Car",
  "courseCode": "ENG540",
  "description": "A real page turner",
  "edition": "3rd",
  "id": "-MQZVV7hxTIUqXYFOxbc",
  "price": "60",
  "sellerName": "...",
  "title": "Introduction to Combustion",
  "uid": "EG4L42ZQ7qS1U8o79n2UUpHYlA02",
}

当我的操作侦听器从 Firebase 检索到新添加的点赞时,他们会将其发送到 likesReducer,它会更新我的商店。但是,在运行console.log(store.getState()) 时,返回的是这样的:

Object {
  "_persist": Object {
    "rehydrated": true,
    "version": -1,
  },
  "likesReducer": Object {
    "likes": Array [
      ChildrenNode {
        "children_": SortedMap {
          "comparator_": [Function NAME_COMPARATOR],
          "root_": LLRBNode {
            "color": false,
            "key": "-MQZVHl3_gzSwFKpltF9",
            "left": LLRBNode {
              "color": false,
              "key": "-MQZV1mo0QQ4nktIw6wr",
              "left": LLRBEmptyNode {},
              "right": LLRBEmptyNode {},
              "value": ChildrenNode {
                "children_": SortedMap {
                  "comparator_": [Function NAME_COMPARATOR],
                  "root_": LLRBNode {
                    "color": false,
                    "key": "price",
                    "left": LLRBNode {
                      "color": true,
                      "key": "dateTime",
                      "left": LLRBNode {
                        "color": false,
                        "key": "condition",
                        "left": LLRBNode {
                          "color": false,
                          "key": "author",
                          "left": LLRBEmptyNode {},
                          "right": LLRBEmptyNode {},
                          "value": LeafNode {
                            "lazyHash_": null,
                            "priorityNode_": ChildrenNode {
                              "children_": SortedMap {
                                "comparator_": [Function NAME_COMPARATOR],
                                "root_": LLRBEmptyNode {},
                              },
                              "indexMap_": IndexMap {
                                "indexSet_": Object {
                                  ".priority": PriorityIndex {},
                                },
                                "indexes_": Object {
                                  ".priority": Object {},
                                },
                              },
                              "lazyHash_": "",
                              "priorityNode_": null,
                            },
                            "value_": "James Stewart",
                          },
                        },
                        "right": LLRBNode {
                          "color": false,
                          "key": "courseCode",
                          "left": LLRBEmptyNode {},
                          "right": LLRBEmptyNode {},
                          "value": LeafNode {
                            "lazyHash_": null,
                            "priorityNode_": ChildrenNode {
                              "children_": SortedMap {
                                "comparator_": [Function NAME_COMPARATOR],
                                "root_": LLRBEmptyNode {},
                              },
                              "indexMap_": IndexMap {
                                "indexSet_": Object {
                                  ".priority": PriorityIndex {},
                                },
                                "indexes_": Object {
                                  ".priority": Object {},
                                },
                              },
                              "lazyHash_": "",
                              "priorityNode_": null,
                            },
                            "value_": "MTH103",
                          },
                        },
"value": LeafNode {
                          "lazyHash_": null,
                          "priorityNode_": ChildrenNode {
                            "children_": SortedMap {
                              "comparator_": [Function NAME_COMPARATOR],
                              "root_": LLRBEmptyNode {},
                            },
                            "indexMap_": IndexMap {
                              "indexSet_": Object {
                                ".priority": PriorityIndex {},
                              },
                              "indexes_": Object {
                                ".priority": Object {},
                              },
                            },
                            "lazyHash_": "",
                            "priorityNode_": null,
                          },
                          "value_": "New/Like New",
                        },
                      },
                      "right": LLRBNode {
                        "color": false,
                        "key": "edition",
                        "left": LLRBNode {
                          "color": false,
                          "key": "description",
                          "left": LLRBEmptyNode {},
                          "right": LLRBEmptyNode {},
                          "value": LeafNode {
                            "lazyHash_": null,
                            "priorityNode_": ChildrenNode {
                              "children_": SortedMap {
                                "comparator_": [Function NAME_COMPARATOR],
                                "root_": LLRBEmptyNode {},
                              },
                              "indexMap_": IndexMap {
                                "indexSet_": Object {
                                  ".priority": PriorityIndex {},
                                },
                                "indexes_": Object {
                                  ".priority": Object {},
                                },
                              },
                              "lazyHash_": "",
                              "priorityNode_": null,
                            },
                            "value_": "Only used a few times - no markings at all!",
                          },
                        },
                        "right": LLRBNode {
                          "color": false,
                          "key": "id",
                          "left": LLRBEmptyNode {},
                          "right": LLRBEmptyNode {},
                          "value": LeafNode {
                            "lazyHash_": null,
                            "priorityNode_": ChildrenNode {
                              "children_": SortedMap {
                                "comparator_": [Function NAME_COMPARATOR],
                                "root_": LLRBEmptyNode {},
                              },
                              "indexMap_": IndexMap {
                                "indexSet_": Object {
                                  ".priority": PriorityIndex {},
                                },
                                "indexes_": Object {
                                  ".priority": Object {},
                                },
                              },
                              "lazyHash_": "",
                              "priorityNode_": null,
                            },
                            "value_": "-MQZV1mo0QQ4nktIw6wr",
                          },
                        },
                        "value": LeafNode {
                          "lazyHash_": null,
                          "priorityNode_": ChildrenNode {
                            "children_": SortedMap {
                              "comparator_": [Function NAME_COMPARATOR],
                              "root_": LLRBEmptyNode {},
                            },
                            "indexMap_": IndexMap {
                              "indexSet_": Object {
                                ".priority": PriorityIndex {},
                              },
                              "indexes_": Object {
                                ".priority": Object {},
                              },
                            },
                            "lazyHash_": "",
                            "priorityNode_": null,
                          },
                          "value_": "8th",
                        },
                      },"value": ChildrenNode {
                        "children_": SortedMap {
                          "comparator_": [Function NAME_COMPARATOR],
                          "root_": LLRBNode {
                            "color": false,
                            "key": "time",
                            "left": LLRBNode {
                              "color": true,
                              "key": "date",
                              "left": LLRBEmptyNode {},
                              "right": LLRBEmptyNode {},
                              "value": LeafNode {
                                "lazyHash_": null,
                                "priorityNode_": ChildrenNode {
                                  "children_": SortedMap {
                                    "comparator_": [Function NAME_COMPARATOR],
                                    "root_": LLRBEmptyNode {},
                                  },
                                  "indexMap_": IndexMap {
                                    "indexSet_": Object {
                                      ".priority": PriorityIndex {},
                                    },
                                    "indexes_": Object {
                                      ".priority": Object {},
                                    },
                                  },
                                  "lazyHash_": "",
                                  "priorityNode_": null,
                                },
                                "value_": "001082021",
                              },
                            },
                            "right": LLRBEmptyNode {},
                            "value": LeafNode {
                              "lazyHash_": null,
                              "priorityNode_": ChildrenNode {
                                "children_": SortedMap {
                                  "comparator_": [Function NA...(truncated to the first 10000 characters)

我不知道发生了什么,但是从早期的错误中,大量随机弹出的东西通常是我如何处理 Promises/async 问题的问题......

这是我的 Action 侦听器和 likesReducer 的代码:

//Updates local redux store with Firebase upon app startup
export function getLikesThunk() {
  return (dispatch) => {
    firebase
      .database()
      .ref("users/" + firebase.auth().currentUser.uid + "/likes")
      .once("value", (snap) => {
        return snap;
      })
      .then((likes) => {
        dispatch(getLikes(likes));
      });
  };
}

//Sets up a Listener for newly added Likes on Firebase, to update the store
export function watchLikeAddedEvent(dispatch) {
  firebase
    .database()
    .ref("users/" + firebase.auth().currentUser.uid + "/likes")
    .on("child_added", (snap) => {
      dispatch(addLike(snap.val()));
    });
}

减速器

const initialState = {
  likes: [],
};
const likesReducer = (state = initialState, action) => {
  switch (action.type) {
    case GET_LIKES:
      console.log(
        "                                                             GOTTEN!!!"
      );
      var l = convertObjStateToArray(action.payload);
      return { ...state, likes: l };
    case ADD_LIKE:
      console.log(action.payload);
      var l = state.likes;
      l.push(action.payload);
      console.log(
        "                                                             ADDDED!!!"
      );
      return { ...state, likes: l };
    case REMOVE_LIKE:
      var l = state.likes; //should already be an array??
      return {
        ...state,
        likes: l.filter((like) => like.id !== action.payload.id),
      };
    default:
      return state;
  }
};

const convertObjStateToArray = (state) => {
  var stateObj = state;
  var stateArray = [];

  for (const [key, value] of Object.entries(stateObj)) {
    stateArray.push(value);
  }
  return stateArray;
};

【问题讨论】:

  • return snap 里面的 getLikesThunk 是故意的吗?还是应该是return snap.val()

标签: javascript firebase react-native redux promise


【解决方案1】:

LLRBEmptyNode appears to be a Firestore object of some kind。所以,这符合你所说的你正在做的事情。

注意we strongly recommend against putting non-serializable values into the Redux store - 你真的应该只保存普通的 JS 对象和数组。

此外,您当前的逻辑在 ADD_LIKE 案例中存在错误,因为您正在改变现有状态。

最后,请注意,您确实应该使用our official Redux Toolkit package,它既可以简化您的 Redux 逻辑,又可以捕获类似的常见错误。

【讨论】:

  • 感谢您的指点,我对这个和自学很陌生,所以我不知道正确的方法是什么。我会看看这些链接!
  • 嗨,我对您提供的链接做了一些阅读,我对如何避免发送更新 redux 存储的承诺感到有点困惑,因为数据是从火力基地。这种情况有最佳实践吗?我正在尝试保存一个普通对象,如 OP 中所示,但我的 firebase Thunk 返回一个似乎没有正确处理的承诺,我认为?
  • 更新:在记录了一些点之后,我注意到在我的辅助函数 convertObjStateToArray 中发生了奇怪的对象内容,该对象在作为 state 传入时是正确的,但在我的 for 循环中的某处 @987654328 @ 成为你在我的 OP 中看到的一团糟。不知道发生了什么,因为我在一个简单的对象上单独测试了这个方法并且它工作正常?如果这是一个明显的错误,我很抱歉
猜你喜欢
  • 1970-01-01
  • 2018-01-08
  • 1970-01-01
  • 2020-01-04
  • 2022-07-05
  • 2019-06-18
  • 1970-01-01
  • 2017-08-22
  • 1970-01-01
相关资源
最近更新 更多