Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mongoose V5.5.3+ introduced .remove() deprecation #281

Open
JellyPies opened this issue Dec 27, 2023 · 0 comments
Open

Mongoose V5.5.3+ introduced .remove() deprecation #281

JellyPies opened this issue Dec 27, 2023 · 0 comments

Comments

@JellyPies
Copy link

JellyPies commented Dec 27, 2023

PROBLEM
As of Mongoose V5.5.3+, use of the .remove() function on documents was deprecated, and should no longer be used, it will throw an error for a non-existent function.

ERROR
ERROR: POST /v1/auth/logout 500 - 43.461 ms - message: refreshTokenDoc.remove is not a function

FIX
Anyone updating their mongoose with this project should use .deleteOne() instead. This project utilizes the deprecated .remove() in two locations within auth.service.js, and one location in the user.service.js

auth.service.js LINES 27 - 33

const logout = async (refreshToken) => {
  const refreshTokenDoc = await Token.findOne({ token: refreshToken, type: tokenTypes.REFRESH, blacklisted: false });
  if (!refreshTokenDoc) {
    throw new ApiError(httpStatus.NOT_FOUND, 'Not found');
  }
  await refreshTokenDoc.remove(); // DEPRECATED, REPLACE WITH await refreshTokenDoc.deleteOne();
};

auth.service.js LINES 40 - 52

const refreshAuth = async (refreshToken) => {
  try {
    const refreshTokenDoc = await tokenService.verifyToken(refreshToken, tokenTypes.REFRESH);
    const user = await userService.getUserById(refreshTokenDoc.user);
    if (!user) {
      throw new Error();
    }
    await refreshTokenDoc.remove(); // DEPRECATED, REPLACE WITH await refreshTokenDoc.deleteOne();
    return tokenService.generateAuthTokens(user);
  } catch (error) {
    throw new ApiError(httpStatus.UNAUTHORIZED, 'Please authenticate');
  }
};

user.service.js LINES 73 - 80

const deleteUserById = async (userId) => {
  const user = await getUserById(userId);
  if (!user) {
    throw new ApiError(httpStatus.NOT_FOUND, 'User not found');
  }
  await user.remove(); // DEPRECATED, REPLACE WITH await user.deleteOne();
  return user;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant