首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Redux-Thunk的调度

Redux-Thunk的调度
EN

Stack Overflow用户
提问于 2021-02-12 13:37:23
回答 1查看 85关注 0票数 0

未登录(承诺)错误:状态代码400请求失败

我需要向数据库发出页面请求,以便登录到系统中,但是我已经太困惑了,不知道如何删除这个错误。在此之前,错误“操作必须是普通对象。请使用自定义中间件进行异步操作。”之后,我连接了Redux,出现了当前的错误。

行为

代码语言:javascript
复制
export const auth = (email, password, isLogin) => {
  return async(dispatch) => {
    dispatch(authData())
    
    let url = 'https://identitytoolkit.googleapis.com/v1/accounts:signUp?key=AIzaSyAU8gNE0fGG8z9zqUyh68Inw9_RzljhCCs'

    if (isLogin) {
      url = 'https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key=AIzaSyAU8gNE0fGG8z9zqUyh68Inw9_RzljhCCs'
    }
    const response = await axios.post(url, authData)
    console.log(response.data)
  }
}

const authData = (email, password, returnSecureToken = true) => ({
  type: 'LOGIN',
  email,
  password,
  returnSecureToken
})

组件

代码语言:javascript
复制
loginHandler = () => {
  this.props.auth(
    this.props.AuthMail,
    this.props.AuthPass,
    true
  )
}

registerHandler = () => {
  this.props.auth(
    this.props.AuthRegMail,
    this.props.AuthRegPass,
    false
  )
}

const mapDispatchToProps = dispatch => {
  return {
    auth: (email, password, isLogin) => dispatch(auth(email, password, isLogin))
  }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-02-12 13:51:07

代码语言:javascript
复制
// You forgot to add the arguments to authData function
dispatch(authData())
// Here you are passing in a function as the second argument
const response = await axios.post(url, authData)

应该是这样的:

代码语言:javascript
复制
export const auth = (email, password, isLogin) => {
  return async (dispatch) => {    
    const url = isLogin ? 'example.com/login' : 'example.com/signup';
    const response = await axios.post(url, {
      email,
      password,
      returnSecureToken: true,
    });
    console.log(response.data);
    // Handle this action somewhere to store the signed in user data in redux
    dispatch({
      type: "LOGIN",
      payload: response.data
    })
  }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66172860

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档