我想在下面的功能中访问已登录用户的详细信息。起初它起作用了,但是现在我得到了'TypeError:无法读取未定义的属性'data'。一切正常,但数据库返回空响应
const authoMiddleware = (req, res, next) => {
let idToken;
if(req.headers.authorization && req.headers.authorization.startsWith('Bearer ')) {
idToken = req.headers.authorization.split('Bearer ')[1];
} else {
console.error('No token found')
return res.status(403).json({message: 'Not authorized'});
}
admin.auth().verifyIdToken(idToken)
.then(decodedToken => {
req.user = decodedToken;
console.log(decodedToken);
return db.collection('users')
.where('userId', '==', req.user.uid)
.limit(1)
.get();
})
.then(data => {
console.log(data.docs)
req.user.email = data.docs[0].data().email;
req.user.name = data.docs[0].data().firstName + ' ' + data.docs[0].data().lastName;
req.user.phoneNumber = data.docs[0].data().phoneNumber;
return next();
})
.catch(err => {
console.error('Error while verifying token', err);
return res.status(403).json(err);
})
}