[英]Node.js internal 500 server error “cannot read id of undefined”
I originally had a 500 internal server error(cannot read id of undefined) coming from this endpoint:我最初有一个来自该端点的 500 内部服务器错误(无法读取未定义的 id):
router.get('/myvenues', validateSession,(req, res) => {
Venue.findAll({ where: {userId: req.user.id }})
.then(venue => res.status(200).json(venue))
.catch(err => res.status(500).json(err));
});
and from my validatesession.js I console.logged the decoded Token and got undefined:从我的 validatesession.js 我 console.logged 解码的 Token 并得到未定义:
jwt.verify(token, process.env.JWT_SECRET, (err, decodedToken) => {
console.log('INVALVID TOKEN:', decodedToken)
if (!err && decodedToken) {
User.findOne({ where: { id: decodedToken.id}})
.then(user => {
if (!user) throw 'err';
req.user = user;
return next();
})
.catch(err => next(err))
} else {
req.errors = err;
next();
}
})
}
Then I created an async function around the fetch which solved the undefined decodedToken error:然后我在 fetch 周围创建了一个异步 function ,它解决了 undefined decodedToken 错误:
INVALVID TOKEN: { id: 3, iat: (this is a number), exp: (this is a number)}
//In node.js terminal
But now the endpoint is being called multiple thousand times a minute whenever i hit that endpoint, AND theres no error.但是现在,每当我到达该端点时,该端点每分钟都会被调用数千次,并且没有错误。
Here is the fetch:这是获取:
const fetchAll = () => {
fetch(`${APIURL}/venues/myvenues`, {
method: 'GET',
headers: {
'Content-Type' : 'application/json',
'Authorization' : props.token,
}
})
.then(res => (res.ok ? res : Promise.reject(res)))
.then(myData => myData.json())
.then(data => {
console.log(data)
setVenues(data)})
.catch(err => console.warn(err))
console.log(props.token)
}
useEffect(() => fetchAll(), [])
Im new to stack overflow so if I goofed and anymore info is needed I would be happy to give it!我是堆栈溢出的新手,所以如果我搞砸了并且需要更多信息,我很乐意提供!
try to console log your Request, and make sure you are correctly sending the user object with his id.尝试通过控制台记录您的请求,并确保您正确发送用户 object 及其 ID。
router.get('/myvenues', validateSession,(req, res) => {
console.log(req)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.