0

感谢您提前阅读我的问题。我正在使用 React 的dvaAnt Design Mobile 处理电话注册功能。

在发送验证码之前,我会判断手机是否已经注册。如果是,它会吐司“此手机已注册”。

现在,返回值是正确的:

const mapStateToProps = (state) => {
  console.log(state.register.message)
}
// {code: 221, message: "This phone has been registered"}

所以我把它写成:

const mapStateToProps = (state) => ({
  returnData: state.register.message
})

然后当我单击按钮时,它将调度一个动作(发送请求):

  getVerifyCode() {
    const { form, returnData } = this.props;
    const { getFieldsValue } = form;
    const values = getFieldsValue();
    this.props.dispatcher.register.send({
        phone: values.phone,
        purpose: 'register',
      })
      // if(returnData.code === 221){
      //   Toast.fail("This phone has been registered", 1);
      // } else {
      //   Toast.success("Send verify code successfully", 1);
      // }
  }

但是当我尝试根据返回值添加 if...else 条件时

   if(returnData.code === 221){
     Toast.fail("This phone has been registered", 1);
   } else {
     Toast.success("Send verify code successfully", 1);
   }

只得到错误:

未捕获(承诺中)类型错误:无法读取未定义的属性“代码”

我认为这是关于异步的问题并尝试使用异步等待:

  async getVerifyCode() {
    ...
    await this.props.dispatcher.register.send({
        phone: values.phone,
        purpose: 'register',
      })
  }

但是得到同样的错误

无法读取未定义的属性“代码”

我想知道为什么以及如何解决这个问题?

补充:这是模型

import * as regiserService from '../services/register';

export default {
  namespace: 'register',
  state: {},
  subscriptions: {
  },
  reducers: {
    save(state, { payload: { data: message, code } }) {
      return { ...state, message, code };
    },
  },
  effects: {
    *send({ payload }, { call, put }) {
      const { data } = yield call(regiserService.sendAuthCode, { ...payload });
      const message = data.message;
      yield put({ type: 'save', payload: { data },});
    },
  },
};
4

1 回答 1

0

处理模型中的条件解决了这个问题:

*send({ payload }, { call, put }) {
  const { data } = yield call(regiserService.sendAuthCode, { ...payload });
  if(data.code === 221){
    Toast.fail("This phone has been registered", 1);
  } else {
    Toast.success("Send verify code successfully", 1);
  }
    yield put({ type: 'save', payload: { data }});
  }
于 2018-05-02T11:07:32.710 回答