【发布时间】:2020-08-19 01:35:58
【问题描述】:
编辑:我在尝试访问我的服务器时收到以下错误:
POST http://localhost:3001/user/login500(内部服务器错误)
我不确定我哪里出错了,这个错误会继续发生:
反应:
export default class Account extends React.Component {
constructor() {
super();
this.state = {
isLoggedIn: false,
};
this.login = this.login.bind(this);
}
componentDidMount() {
const cookies = new Cookies();
const user = cookies.get('user');
const pass = cookies.get('pass');
this.setState({processing: true})
fetch('http://localhost:3001/user/login', {
credentials : 'omit',
method : 'POST',
body : JSON.stringify({
username : user,
password : pass
})
})
.then(res => res.json())
.then(json => {
// If the login was successful
if (json.success) {
this.setState ({
isLoggedIn: true
})
}
// Otherwise
else {
this.setState({
errorMessage: 'Invalid username or password',
processing : false
});
}
})
.catch(() => {
this.setState({
errorMessage: 'An unknown error occurred',
processing : false
});
});
}
render() {
if(this.state.isLoggedIn) {
return (<p> Logged In</p>);
}
else {
return (<p> Not Logged In</p>);
}
}}
快递:
router.post('/login', (req, res) => {
return User
.findOne({username: req.body.username})
.then (user => user.authenticate(req.body.password))
.then (auth => {
if (auth.user !== false) {
req.session.user = auth.user.user
}
res.json({success: auth.user !== false})
})
.catch(() => res
.status(500)
.json({success: false})
);
});
这个错误没有提供太多关于我可能做错的信息,但它可能与 cors 有关。
【问题讨论】:
-
@AtinSingh 我看到了这个,但是如果我使用 cors 包,我会收到以下错误:
POST http://localhost:3001/user/login net::ERR_CONNECTION_REFUSED -
.
catch((err) => res //add err here and console.log .status(500) .json({success: false}) );你能做到这一点并告诉我们结果吗?如果 err 太大,您可以告诉我们相关部分。 (可能在 err.response.data 中) -
我的控制台显示:
Cannot read property 'authenticate' of null