React Native&Redux-错误:动作必须是纯对象。使用自定义中间件进行异步操作

时间:2019-10-23 19:17:42

标签: javascript reactjs react-native redux state

我正在尝试使用Redux构建一个本机应用程序,并且由于我的Actions文件出现以下错误:

错误:动作必须是普通对象。使用自定义中间件执行异步操作

有人知道这里发生了什么吗?这是相关代码:

import axios from 'axios' //http client
import {API_URL} from '../utils/constants'

export const FETCH_USER = 'fetch_user'

export const editProfileUser = async (email, password, name, location, aboutMe, memberSince, 
   picture) => {
 try{
 const response = await axios({
 method: 'POST',
 url: `${API_URL}/api/get_token`,
 data: {
  email,
  password
 }
 })
 const {token} = response.data
 const userResponse = await axios({
  method: 'POST',
  url: `${API_URL}/api/edit_user_profile`,
  headers: {
    Authorization: `${token}`
  },
  data: {
    name,
    location,
    aboutMe,
    memberSince,
    picture
  }
})

console.log("userResponse.data", userResponse.data)

return (
  {
    type: FETCH_USER,
    payload: {
      token,
      email,
      password
    }
  }
)


} catch(err){
console.log("Exception in actions/user/editProfileUser err", err)
}
}

1 个答案:

答案 0 :(得分:0)

好像您正在尝试执行Redux之外的异步操作,您要么需要在外部处理请求,要么使用redux-observableredux-saga之类的异步redux库

您可以做的就是在诺言完成后立即采取行动:

axios({
      method: 'POST',
      url: `${API_URL}/api/edit_user_profile`,
      headers: {
        Authorization: `${token}`
      },
      data: {
        name,
        location,
        aboutMe,
        memberSince,
        picture
      }
    }).then(({token, email, password}) => {
     // Dispatch your action with the values you want to store
     dispatch(editProfileUser(token, email, password))
})

来自redux文档:

  

操作是从您的计算机发送数据的信息有效载荷   应用程序到您的商店。它们是唯一的信息来源   商店。

操作仅负责发送数据,而不负责检索