Javascript 使此异步/等待逻辑工作时出现问题
我的React app.js中有以下功能Javascript 使此异步/等待逻辑工作时出现问题,javascript,reactjs,Javascript,Reactjs,我的React app.js中有以下功能 login=(电子邮件、密码)=>{ axios({ 方法:“张贴”, url:“http://localhost:3003/login", 数据:qs.stringify({email,password}), 标题:{ “内容类型”:“application/x-www-form-urlencoded;charset=utf-8” } }) 。然后(res=>{ 控制台日志(res); //accestoken设置为cookie以检查路由 Cookie
login=(电子邮件、密码)=>{
axios({
方法:“张贴”,
url:“http://localhost:3003/login",
数据:qs.stringify({email,password}),
标题:{
“内容类型”:“application/x-www-form-urlencoded;charset=utf-8”
}
})
。然后(res=>{
控制台日志(res);
//accestoken设置为cookie以检查路由
Cookies.set(“accesstoken”,res.data.accesstoken);
//在重定向到登录之前,我们必须手动检查accesstoken,否则它将允许导航,因为它不是一个组件
isAuthenticated()。然后(结果=>{
如果(结果==真){
这是我的国家(
{isAuthenticated:true,authenticationChecked:true},
() => {
this.props.history.push(“/”);
}
);
}否则{
这是我的国家({
I认证:错误,
authenticationChecked:true
});
如果(res.data==“密码不正确”){
console.log(“me va a HACHER un return del pass”);
返回“密码不正确”;
}else if(res.data==“用户不存在”){
返回“用户不存在”;
}
}
});
})
.catch(错误=>{
控制台日志(err);
});
};
我把它作为道具传递到我的一条路线
<Route exact path="/login" render={(props) => <LoginPage login={this.login} {...props} />} />
但是由于它是一个作为道具传递的函数,我真的不知道如何在那里实现wait,我尝试将登录函数设置为async,然后像这样等待道具
const loginStatus = await this.props.login(email, password)
但我不会让我这么做的
async handleSubmit(event) {
const { email, password } = this.state;
var loginStatus = await this.props.login(email, password)
console.log("estado del login")
console.log(loginStatus); //this always evaluates to undefined, even if the return happens
if (loginStatus == "Password not correct"){
this.setState({
wrongPassword : true
})
}else if(loginStatus == "User doesnt exist"){
this.setState({
wrongUser : true
})
}
event.preventDefault();
}
你的功能应该是这样的
您的功能应该是这样的必须创建一个承诺
app.js
login = (email, password) => {
return new Promise( (resolve, reject) => {
axios({
method: 'post',
url: 'http://localhost:3003/login',
data: qs.stringify({ email, password }),
headers: {
'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
}
}).then((res) => {
console.log(res)
//the accestoken is set as a cookie in order to check routes
Cookies.set('accesstoken', res.data.accesstoken);
//we have to check the accesstoken manually before redirecting it to login, or else it will allow navigate since its not a <PrivateRoute> component
isAuthenticated().then((result) => {
if (result === true) {
this.setState({ isAuthenticated: true, authenticationChecked: true }, () => {
this.props.history.push('/');
})
resolve(true);
} else {
this.setState({ isAuthenticated: false, authenticationChecked: true })
if (res.data == "Password not correct") {
console.log("me va a hacer un return del pass")
reject("Password not correct");
} else if (res.data == "User doesnt exist") {
reject("User doesnt exist")
}
}
});
}).catch(err => {
console.log(err)
})
})
}
必须做出承诺
app.js
login = (email, password) => {
return new Promise( (resolve, reject) => {
axios({
method: 'post',
url: 'http://localhost:3003/login',
data: qs.stringify({ email, password }),
headers: {
'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
}
}).then((res) => {
console.log(res)
//the accestoken is set as a cookie in order to check routes
Cookies.set('accesstoken', res.data.accesstoken);
//we have to check the accesstoken manually before redirecting it to login, or else it will allow navigate since its not a <PrivateRoute> component
isAuthenticated().then((result) => {
if (result === true) {
this.setState({ isAuthenticated: true, authenticationChecked: true }, () => {
this.props.history.push('/');
})
resolve(true);
} else {
this.setState({ isAuthenticated: false, authenticationChecked: true })
if (res.data == "Password not correct") {
console.log("me va a hacer un return del pass")
reject("Password not correct");
} else if (res.data == "User doesnt exist") {
reject("User doesnt exist")
}
}
});
}).catch(err => {
console.log(err)
})
})
}
您是否已将函数标记为async?this.props.login没有返回承诺,因此在其上使用Wait没有任何用处您需要返回axios CallReact抛出的错误是什么?@GauravBhusare
解析错误:如果我设置const loginStatus=Wait this.props.login,则无法在react中的异步函数
外部使用关键字“wait”(电子邮件、密码)
您是否将函数标记为async?this.props.login没有返回承诺,因此在其上使用Wait没有任何用处您需要返回axios CallReact抛出时出现了什么错误?@GauravBhusare解析错误:如果我设置const loginStatus=Wait this.props,则无法在react中的异步函数外部使用关键字'Wait'.login(电子邮件、密码)
仍然返回未定义的您应该从if(result==true)条件返回内容仍然返回未定义的您应该从if(result==true)条件返回内容