Node.js API路由适用于邮递员,但不适用于客户端应用程序
我有以下路径,由/api/mentors/details调用Node.js API路由适用于邮递员,但不适用于客户端应用程序,node.js,reactjs,mongodb,api,express,Node.js,Reactjs,Mongodb,Api,Express,我有以下路径,由/api/mentors/details调用 router.route('/details').put( asyncHandler(async (req, res) => { let token; if ( req.headers.authorization && req.headers.authorization.startsWith('Bearer') ) { try { c
router.route('/details').put(
asyncHandler(async (req, res) => {
let token;
if (
req.headers.authorization &&
req.headers.authorization.startsWith('Bearer')
) {
try {
console.log('This wants to be claled');
token = req.headers.authorization.split(' ')[1];
const decoded = jwt.verify(token, 'abc123');
const user = await User.findById(decoded.id).select('-password');
if (user) {
const mentor = await Mentor.findOne({user_linked_to: user._id});
if (mentor) {
mentor.name = req.body.name || mentor.name;
mentor.email = req.body.email || mentor.email;
mentor.bio = req.body.bio || mentor.bio;
mentor.password = req.body.password || mentor.password;
mentor.university = req.body.university || mentor.university;
mentor.major = req.body.major || mentor.major;
mentor.type = req.body.type || mentor.type;
mentor.housing = req.body.housing || mentor.housing;
mentor.clubs = req.body.clubs || mentor.clubs;
mentor.days_available =
req.body.days_available || mentor.days_available;
mentor.isAvailable = req.body.isAvailable || mentor.isAvailable;
mentor.image = req.body.image || mentor.image;
mentor.isVerified = false;
mentor.price = req.body.price || mentor.price;
const updatedMentor = await mentor.save();
if (updatedMentor) {
res.status(201).json(updatedMentor);
} else {
res.status(404);
throw new Error('Mentor could not be updated');
}
} else {
res.status(404);
throw new Error('Mentor not found');
}
} else {
res.status(404);
throw new Error('User not found');
}
// needs changing shouldn't really be passing id's through requests
//instead current User's Id should be checked and that's how the authenticated privacy for this route should also be enforced
} catch (error) {
console.error(error);
res.status(401);
throw new Error('Not authorized, token failed');
}
}
})
);
当我使用邮递员呼叫这条路线时,它起作用了,我能够更新各自导师的详细信息
使用以下操作在我的react应用程序中调用相同的路由:
export const updateMentorDetails = (mentor) => async (dispatch, getState) => {
dispatch({
type: 'MENTOR_UPDATE_DETAILS_REQUEST',
});
try {
const {
userLogin: {userInfo},
} = getState();
console.log(userInfo);
const config = {
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${userInfo.token}`,
},
};
console.log(mentor);
const {data} = await axios.put(`/api/mentors/details`, mentor, config);
dispatch({
type: 'MENTOR_UPDATE_DETAILS_SUCCESS',
payload: data,
});
} catch (error) {
dispatch({
type: 'MENTOR_UPDATE_DETAILS_FAIL',
payload:
error.response && error.response.data.message
? error.response.data.message
: error.message,
});
}
};
使用以下数据调用此操作,如下所示:
dispatch(
updateMentorDetails({
id: mentor._id,
name: name,
email: email,
password: password,
days_available: days_available,
bio: bio,
major: major,
housing: housing,
isAvailable: isAvailable,
price: price,
university: university,
type: type,
clubs: clubs,
image: image,
})
但是,当我尝试使用此方法时,我得到一个错误:Not authorized,token failed
我在我的应用程序中使用了一种类似的方法来更新另一种类型的模型,这种方法已经奏效了,但由于某种原因,尽管路线在邮递员中起作用,但这并不是有效的。有人能给我一些帮助吗。试着重新生成一个新的
userInfo.token
,或者将你在邮递员身上使用的相同token复制/粘贴到你的代码中,看看它是否有效