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

error TS2339: Property 'isValid' does not exist on type 'ObjectIdConstructor'. #10938

Closed
ssomlk opened this issue Oct 27, 2021 · 2 comments
Closed

Comments

@ssomlk
Copy link

ssomlk commented Oct 27, 2021

Do you want to request a feature or report a bug?
Bug

What is the current behavior?
I am creating a REST Api and when running the Node server getting the below error
error TS2339: Property 'isValid' does not exist on type 'ObjectIdConstructor'.

If the current behavior is a bug, please provide the steps to reproduce.

Below is my code

import express, { Request, Response } from 'express';
import mongoose from 'mongoose';

import {
  User,
  currentUser,
  requireAuth,
  RestrictToRoles,
  NotFoundError,
} from '@ssomlk-org/distribution-common';

const router = express.Router();

router.delete(
  '/api/users/disable-user/:userId',
  currentUser,
  requireAuth,
  RestrictToRoles('developer', 'admin'),
  async (req: Request, res: Response) => {
    if (!mongoose.Types.ObjectId.isValid(req.params.userId)) {
      throw new NotFoundError('User not found');
    }

    const existingUser = await User.findById(req.params.userId);

    if (!existingUser) {
      throw new NotFoundError('User not found');
    }

    existingUser.status = 'Inactive';
    await existingUser.save();

    const response = {
      status: 'success',
      data: {
        user: existingUser,
      },
    };

    res.status(204).send(response);
  }
);

export { router as disableUserRouter };

Error is due to mongoose.Types.ObjectId.isValid(req.params.userId)

What is the expected behavior?

The server should run without any errors. This was working earlier but suddenly getting this error

image

NOTE: Not using @types/mongoose anymore

{
  "compilerOptions": {
    "target": "es5" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */,
    "module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */,
    "outDir": "./build" /* Redirect output structure to the directory. */,
    "rootDir": "./src" /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */,
    "downlevelIteration": true /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */,
    "strict": false /* Enable all strict type-checking options. */,
    "esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,
    "skipLibCheck": true /* Skip type checking of declaration files. */,
    "forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
  }
}

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
node:15.11.0-alpine3.13
mongoose: 5.13.7
typescript: 4.3.5

@ssomlk
Copy link
Author

ssomlk commented Oct 27, 2021

Seems like this is connected.
#10678

Any thoughts

@ssomlk
Copy link
Author

ssomlk commented Oct 27, 2021

Issue fixedby updating the mongoose to 5.13.9

@ssomlk ssomlk closed this as completed Oct 27, 2021
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