Javascript TypeError:undefined不是对象(计算';"this.props.auth(values.username,values.password)。然后';)
我正在开发一个ReactJS应用程序 我得到以下错误“TypeError:undefined不是对象(计算'\u this.props.auth(values.username,values.password).then')” 当“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
返回新承诺
”在“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()
不会返回任何内容。要使代码正常工作,它应该返回一个承诺
。还有身份验证状态
指的是什么以及它在哪里定义?感谢您的解决方案+解释!