推送不是功能,Redux

时间:2019-12-06 17:07:44

标签: javascript reactjs redux react-redux

我有一个要在其他组件中使用的状态组件,它必须在多个组件中使用,因此我将其切换为redux。现在使用与我的组件中相同的功能,我收到错误消息“ newUsers.push不是一个功能”

以下是我的还原操作:

import { FETCH_USERS_TO_ADD } from './types';
import axios from 'axios'


export const fetchUsersToAdd = () => dispatch => {
var userBox = [];
var newUserBox = [];
let newUserIds = {};
let newUsers = [];

    axios.all([
        axios.get('/updateUserList'),
        axios.get('/userInformation')
        ])
        .then(axios.spread((newUsers, currentUsers) => {
            userBox.push(newUsers.data)
            newUserBox.push(currentUsers.data)
            for (let newUser of newUserBox){
                newUserIds[newUser.id] = newUser.id
              }
              for (let user of userBox){
                if(!newUserIds[user.staffID]){
                console.log(user)
                  **newUsers.push(user)**
                }
              }
        })).then(dispatch({
            type: FETCH_USERS_TO_ADD,
            payload: newUsers
        }))


}

“我的课程”组件中的以下代码没有给我这个错误: 让newUserIds = {}; 让newUsers = [];

this.state = {
  userBox : [],
  newUserBox : [],
  usersToBeAdded:[],
}

componentDidMount(){

  axios.all([
    axios.get('/updateUserList'),
    axios.get('/userInformation')
  ])
  .then(axios.spread((newUsers, currentUsers) => {
    this.setState({
      userBox : newUsers.data,
      newUserBox : currentUsers.data,
    })
  }))

}

checkForNewUsers = (e) => {

  for (let newUser of this.state.newUserBox){
    newUserIds[newUser.id] = newUser.id
  }
  for (let user of this.state.userBox){
    if(!newUserIds[user.staffID]){
      newUsers.push(user)
    }
  }
  this.setState({
    usersToBeAdded:newUsers
  })
  console.log(newUsers)
}

<UpdateUsersButton onClick={this.checkForNewUsers}/>

在控制台用户时,它会返回一个如下数组:

Array(10)
0: {staffID: 1, dept: "Junior Web Developer", email: "manasaN@tpionline.com", name: "manasa", password: "$2y$10$/zYS7OhzwdLOi6Slzc3xxxxxiY0y1J6MjnLCN24GmZ3rMHWUS"}
1: {staffID: 2, dept: "Web Developer", email: "juliof@tpionline.net", name: "Julio Fajardo", password: "$2y$10$MphAC8aRY2uzs1Zxxxxxnd7t0KukEkvGbx5Y4van.Da6"}

1 个答案:

答案 0 :(得分:2)

我认为这是因为newUsers在箭头功能中被遮盖了,而不是您认为的那样。不要重复使用变量名。

尝试一下:

import { FETCH_USERS_TO_ADD } from './types';
import axios from 'axios'


export const fetchUsersToAdd = () => dispatch => {
var userBox = [];
var newUserBox = [];
let newUserIds = {};
let newUsersArr = []; // Make unique

    axios.all([
        axios.get('/updateUserList'),
        axios.get('/userInformation')
        ])
        .then(axios.spread((newUsers, currentUsers) => { // Was shadowed here
            userBox.push(newUsers.data)
            newUserBox.push(currentUsers.data)
            for (let newUser of newUserBox){
                newUserIds[newUser.id] = newUser.id
              }
              for (let user of userBox){
                if(!newUserIds[user.staffID]){
                console.log(user)
                  newUsersArr.push(user)
                }
              }
        })).then(dispatch({
            type: FETCH_USERS_TO_ADD,
            payload: newUsersArr
        }))


}