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

Use FilterQuery Type to modify document property throw Error. #10186

Closed
yusunglee2074 opened this issue Apr 28, 2021 · 2 comments
Closed

Use FilterQuery Type to modify document property throw Error. #10186

yusunglee2074 opened this issue Apr 28, 2021 · 2 comments
Labels
typescript Types or Types-test related issue / Pull Request
Milestone

Comments

@yusunglee2074
Copy link

Do you want to request a feature or report a bug?
Bug
What is the current behavior?

Use FilterQuery Type to modify document property.

import { Document, Schema as MongooseSchema } from 'mongoose';

export type UserDocument = User & Document;

export class User {
  email: string;

  password: string;
}

async update(
  filter: FilterQuery<UserDocument>,
  data: UpdateQuery<UserDocument>,
) {
  if (data.password) {
    let password: string;
    data.password = hashPassword(data.passward)
  }
  this.userModel
      .findOneAndUpdate(filter, data.password ? { ...data, password } : data, {
        new: true,
      })
}

Errors.
Property 'password' does not exist on type 'UpdateQuery
Property 'password' does not exist on type 'UpdateWithAggregationPipeline'.

tsconfig.json

{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "target": "es2017",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "incremental": true
  }
}

What is the expected behavior?
I can modify exist UserDocument property.

Node.js: v14.16.0
mongoose: v5.12.5 and v5.12.6 (After 5.12.5 error occurs, after this commit) 61d24d4#commitcomment-50023157

@yusunglee2074
Copy link
Author

yusunglee2074 commented Apr 28, 2021

And I work with nest.js.

This is full error message.

error TS2339: Property 'password' does not exist on type 'UpdateQuery<UserDocument>'.
  Property 'password' does not exist on type 'UpdateWithAggregationPipeline'.
65     if (data.password) {

@IslandRhythms IslandRhythms added the needs clarification This issue doesn't have enough information to be actionable. Close after 14 days of inactivity label May 4, 2021
@vkarpov15 vkarpov15 added this to the 5.12.8 milestone May 4, 2021
@vkarpov15 vkarpov15 added typescript Types or Types-test related issue / Pull Request and removed needs clarification This issue doesn't have enough information to be actionable. Close after 14 days of inactivity labels May 4, 2021
@vkarpov15
Copy link
Collaborator

I can confirm the below script works once c4ba0e9 is applied:

import { Document, Schema as MongooseSchema, FilterQuery, UpdateQuery } from 'mongoose';
  
export type UserDocument = User & Document;

export class User {
  email: string;

  password: string;
}

async function update(
  filter: FilterQuery<UserDocument>,
  data: UpdateQuery<UserDocument>,
) {
  let password = '42';
  this.userModel
      .findOneAndUpdate(filter, data.password ? { ...data, password } : data, {
        new: true,
      })
}

Fix will be in v5.12.8.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
typescript Types or Types-test related issue / Pull Request
Projects
None yet
Development

No branches or pull requests

3 participants