【问题标题】:TypeError: undefined is not a function (evaluating userItems.map)TypeError: undefined is not a function (评估 userItems.map)
【发布时间】:2018-10-14 04:55:03
【问题描述】:

编辑:现在我使用“react-native-navigation@latest”一切都很好。

当我第一次打开应用程序时 它正在正确地获取数据但在第二次刷新之后发生错误

TypeError: undefined is not a function (evalating 'userItems.map')

此错误位于:
在用户中(由 Connect(Users) 创建);
....

....

组件/Users.js

  componentWillMount(){
    this.props.fetchUsers();
  }

  render() {
    const userItems = this.props.users || [];
    const abc = userItems.map((user,index) => (
      <Text key={index}>
      {user.email}
      </Text>
    ));
    return (
      <View>
        {abc}
      </View>
    );
  }
}
const mapStateToProps = state => ({
  users: state.UserReducer.items
})

const mapDispatchToProps = {
  fetchUsers
};


export default connect(mapStateToProps, mapDispatchToProps)(Users);

动作

export function fetchUsers() {
  return dispatch => {
    fetch("http://example.com/v1/users")
      .then(res => res.json())
      .then(users =>
        dispatch({
          type: FETCH_USERS,
          payload: users
        })
      );
  };
}

reducer/userReducer.js

import { FETCH_USERS } from "../Actions/types";

    const initialState = {
      items: []
    };

    const userReducer= (state = initialState, action) => {
        switch(action.type){
            case FETCH_USERS:
                return {
                    ...state,
                    items:action.payload
                }
            default:
                return state;
        }
    }
export default userReducer;

reducer/index.js

import UserReducer from './userReducer';
const AppReducer = combineReducers({
  ...
  UserReducer,
  ...
  ...
  ...
});

export default AppReducer;

后端很好,我用邮递员测试

用户后台

  const router = new express.Router();

  router.route("/users").get((req, res) => {
    User.find({},{password:0}, (err, users) => {
      if (err) {
        return res.status(404).send({message: "error"});
        console.log(err);
      } else {
        return res.status(200).send({users});
      }
    });
  });

响应后端

{
    "users": [
        {
            "dateCreated": "..",
            "dateModified": "...",
            "lastLogin": "...",
            "_id": "...",
            "email": "...",
            "__v": 0
        },
        {
            "dateCreated": "..",
            "dateModified": "...",
            "lastLogin": "...",
            "_id": "...",
            "email": "...",
            "__v": 0
        }
    ]
}

package.json 依赖项

 "dependencies": {
    "asyncstorage": "^1.5.0",
    "es6-promise": "^4.2.4",
    "react": "16.3.1",
    "react-native": "0.55.3",
    "react-native-vector-icons": "^4.6.0",
    "react-navigation": "v1.0.0-beta.26",
    "react-redux": "^5.0.7",
    "redux": "^3.7.2",
    "redux-logger": "^3.0.6",
    "redux-persist": "^5.9.1",
    "redux-thunk": "^2.2.0"
  },

【问题讨论】:

  • 很可能来自您的 api 响应的 users 不是数组
  • 这是一个数组,我很确定,因为在我现在实施我的新项目之前我确实获取了它,但没有工作
  • 您是否在代码中使用 react-navigation [作为依赖项]?

标签: javascript reactjs react-native redux map-function


【解决方案1】:

从给出的代码示例中,您的代码看起来正在使用 react-native-router-flux(它具有 react-navigation 作为依赖项)。这个listed on github 有一个未解决的问题。正如SO answer 中所讨论的,当前的解决方案是使用 beta26 版本 + 的 react-native router-flux。 我怀疑这会解决你的问题。如果没有,那么,值得一试! ..

第..

【讨论】:

  • 我正在使用“react-navigation”:“v1.0.0-beta.26”,
  • 确保您安装了正确的对等依赖项。你用的是纱线还是 npm?我有一次因为其他原因而出现此错误并升级了我的所有依赖项,并更新了 npm 的版本以纠正错误
  • 我尝试删除节点模块并使用 yarn 和 npm 再次安装,两次都出现同样的错误。感谢您尝试提供帮助
  • 我没有手动删除节点模块。我刚刚升级到npm的最新vsn,安装了yarn的最新vsn。然后使用 yarn 升级每个依赖。我想我从项目中删除了依赖项,将它们全局升级并重新添加。
  • 比较您正在使用的依赖项与您的 package.json 中列出的依赖项以及您的 android 文件中列出的依赖项。删除任何不必要的。我发现这很有用..
【解决方案2】:

如果/users 响应是这样的:

{
    "users": [
        {
            "dateCreated": "..",
            "dateModified": "...",
            "lastLogin": "...",
            "_id": "...",
            "email": "...",
            "__v": 0
        }
    ]
}

那么在调度FETCH_USERS时,你应该这样做:(注意payload属性)

export function fetchUsers() {
  return dispatch => {
    fetch("http://example.com/v1/users")
      .then(res => res.json())
      .then(data =>
        dispatch({
          type: FETCH_USERS,
          payload: data.users
        })
      );
  };
}

或者你可以在 reducer 中执行此操作(注意 items 属性):

const userReducer= (state = initialState, action) => {
    switch(action.type){
        case FETCH_USERS:
             return {
                  ...state,
                  items: action.payload.users 
             }
             default:
             return state;
    }
}

【讨论】:

  • 我试过了,但它确实先正确加载了数据,但是当我更改页面或刷新页面时再次出现错误。
  • 这可能与您调用this.props.fetchUsers(); 的位置有关,请尝试在componentDidMount 中调用它而不是componentWillMount `
【解决方案3】:

您确定要在 MapDispatchToProps 中映射您的操作吗? (理想情况下,这篇文章应该是评论,但我还没有评论权限)。

const mapDispatchToProps = (dispatch) => {
    return {
        fetchUsers: () => dispatch(fetchUsers());
    };    
};

【讨论】:

  • mapDispatchToProps 也可以是一个对象! source
猜你喜欢
  • 1970-01-01
  • 2015-06-24
  • 2021-06-21
  • 1970-01-01
  • 2014-08-15
  • 2015-01-14
  • 2014-07-06
  • 2014-09-01
  • 2020-02-15
相关资源
最近更新 更多