I have this route to handle password changes from the client:
router.patch(
"/users/:id/change-password",
checkForAuthentication,
async (req: Request, res: Response) => {
const userId: string = req.params.id;
const oldPassword: string = req.body.oldPassword;
const newPassword: string = req.body.newPassword;
try {
const objectId = new Types.ObjectId(userId);
const hashedNewPassword = await hashPassword(newPassword);
const user = await User.findById(objectId);
if (!user)
return res.status(404).send({ error: "User not found weeweewee" });
if (!(await comparePassword(oldPassword, user.password))) {
return res.status(400).send({ error: "Old password doesn't match" });
}
user.password = hashedNewPassword;
await user.save();
res.status(200).send({ msg: "Password updated successfully" });
} catch (err) {
console.error(err);
return res.status(400).send({ error: "Invalid ID or other error" });
}
}
);
I’m using mutations from Redux Toolkit to handle PATCH requests. How can I tell that the response data being sent shouldn’t belong in RTK query’s data
key, but rather the error
key (e.g.: { data, error }
)? TS IntelliSense shows me the type of const res = await changePasswordWithId({...})
:
const res: {
data: {
error?: string | undefined;
msg?: string | undefined;
};
error?: undefined;
} | {
data?: undefined;
error: FetchBaseQueryError | SerializedError;
}