Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript TypeError:undefined不是对象(计算';"this.props.auth(values.username,values.password)。然后';)_Javascript_Reactjs_Promise - Fatal编程技术网

Javascript TypeError:undefined不是对象(计算';"this.props.auth(values.username,values.password)。然后';)

Javascript TypeError:undefined不是对象(计算';"this.props.auth(values.username,values.password)。然后';),javascript,reactjs,promise,Javascript,Reactjs,Promise,我正在开发一个ReactJS应用程序 我得到以下错误“TypeError:undefined不是对象(计算'\u this.props.auth(values.username,values.password).then')” 当“返回新承诺”在“then”之外时,它会正常工作。尽管如此,我还是想在两个先“然后”的“s”之后,才回报这个承诺 loginActions.js的示例 export const auth = (username, password) => dispatch =&g

我正在开发一个ReactJS应用程序

我得到以下错误“TypeError:undefined不是对象(计算'\u this.props.auth(values.username,values.password).then')”

当“
返回新承诺
”在“then”之外时,它会正常工作。尽管如此,我还是想在两个先“然后”的“s”之后,才回报这个承诺

loginActions.js的示例

export const auth = (username, password) => dispatch => {
  fetch('http://localhost/webservices/login', {
    method: 'post',
    body: JSON.stringify({ username, password })
  })
  .then(res => {
    if(res.ok) {
      console.log("Succeeded.", res);
      return res.json();
    } else {
      console.log("Failed.", res);
      return res.json();
    }
  })
  .then(json => {
    if (json.token) {
      auth_status.value = true;
      return auth_status.value;
    } else {
      auth_status.value = false;
      return auth_status.value;
    }
  })
  .then(function(res){
    return new Promise((resolve, reject) => {
      dispatch({
        type: VERIFY_AUTH,
        payload: res
      });
      resolve();
    })
  })
  .catch(err => {
    console.error(err);
  });
};
  handleSubmit = (e) => {
    e.preventDefault();
    this.props.form.validateFields((err, values) => {
      if (!err) {
        console.log("Received values of form: ", values);
        this.props.auth(values.username, values.password).then(() => {
          if (this.props.auth_status === true) {
            message.success("Welcome!", 3);
            this.setState({
              redirect: true
            });
          } else {
            message.error("The username and password combination is incorrect", 3);
          }
        })
        .catch(err => {
          console.error(err);
        });
      }
    });
  };
login.js的示例

export const auth = (username, password) => dispatch => {
  fetch('http://localhost/webservices/login', {
    method: 'post',
    body: JSON.stringify({ username, password })
  })
  .then(res => {
    if(res.ok) {
      console.log("Succeeded.", res);
      return res.json();
    } else {
      console.log("Failed.", res);
      return res.json();
    }
  })
  .then(json => {
    if (json.token) {
      auth_status.value = true;
      return auth_status.value;
    } else {
      auth_status.value = false;
      return auth_status.value;
    }
  })
  .then(function(res){
    return new Promise((resolve, reject) => {
      dispatch({
        type: VERIFY_AUTH,
        payload: res
      });
      resolve();
    })
  })
  .catch(err => {
    console.error(err);
  });
};
  handleSubmit = (e) => {
    e.preventDefault();
    this.props.form.validateFields((err, values) => {
      if (!err) {
        console.log("Received values of form: ", values);
        this.props.auth(values.username, values.password).then(() => {
          if (this.props.auth_status === true) {
            message.success("Welcome!", 3);
            this.setState({
              redirect: true
            });
          } else {
            message.error("The username and password combination is incorrect", 3);
          }
        })
        .catch(err => {
          console.error(err);
        });
      }
    });
  };

您的操作
auth
没有返回任何内容。异步处理程序中的
return
语句不会为操作本身返回

您需要在您的
auth()
操作中返回一个
Promise
,然后在第三个
中解析自己:

export const auth = (username, password) => dispatch => {
    // instantly return a new promise that 
    // can be resolved/rejected in one of the handlers
    return new Promise((resolve, reject) => {
        fetch('http://localhost/webservices/login', {
            method: 'post',
            body: JSON.stringify({
                username,
                password
            })
        }).then(res => {
            if (res.ok) return res.json();

            // your probably also want to reject here 
            // to handle the failing of the action
            reject();
        }).then(json => {
            if (json.token) {
                auth_status.value = true;
                return auth_status.value;
            } else {
                auth_status.value = false;
                return auth_status.value;
            }
        }).then(res => {
            dispatch({
                type: VERIFY_AUTH,
                payload: res
            });

            // resolve the original promise here
            resolve();
        }).catch(err => console.error(err));
    });
};

作为道具传递的函数
auth()
不会返回任何内容。要使代码正常工作,它应该返回一个
承诺
。还有
身份验证状态
指的是什么以及它在哪里定义?感谢您的解决方案+解释!