Javascript Redux store.getState()返回意外的长对象,可能存在承诺问题

Javascript Redux store.getState()返回意外的长对象,可能存在承诺问题,javascript,firebase,react-native,redux,promise,Javascript,Firebase,React Native,Redux,Promise,我正在尝试为电子商务风格的应用程序设置我的redux商店。我们的数据库是Firebase,我试图在redux存储中模仿Firebase的数据结构。我首先用用户“喜欢”数据对其进行测试,这些数据是结构化的,并作为对象从Firebase返回,如下所示: Object { "author": "Stephen Turns", "condition": "Run Over By Car", "cours

我正在尝试为电子商务风格的应用程序设置我的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检索新添加的like时,它们会将其发送到更新我的存储的
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)
我不知道发生了什么,但是从早期的bug来看,很多像这样的随机出现的东西通常是我如何处理承诺/异步问题的一个问题

以下是我的动作监听器和类似Educer的
的代码:

//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;
};
。这和你说的相符

请注意-您应该只保存普通JS对象和数组

另外,您当前的逻辑在
ADD_LIKE
案例中有一个bug,因为您正在改变现有状态


最后,请注意,您确实应该使用它,这将简化您的Redux逻辑并捕获常见的错误?还是应该是
return snap.val()
?谢谢你的指点,我对这个和自学非常陌生,所以我不知道正确的方法是什么。我来看看这些链接!您好,我读了一些您提供的链接,对于如何避免发送更新redux存储的承诺,我有点困惑,因为数据是从Firebase提取的。对于这种情况是否有最佳实践?我正试图保存一个普通对象,如OP中所示,但我的firebase Thunk返回了一个似乎没有正确处理的承诺,我想?更新:在记录了一些点之后,我注意到我的助手函数
convertObjStateToArray
中出现了奇怪的对象内容,该对象在作为
状态传递时是正确的,但是在我的for循环
stateArray
中的某个地方变成了你在我的OP中看到的混乱。不确定发生了什么,因为我在一个简单的对象上单独测试了这个方法,它工作得很好?如果这是一个明显的错误,我很抱歉