Skip to content

error when using property setter #10799

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

Closed
GamelSadek opened this issue Sep 28, 2021 · 2 comments
Closed

error when using property setter #10799

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

Comments

@GamelSadek
Copy link

Do you want to request a feature or report a bug?
bug
What is the current behavior?
when you use a function to set a property value in schema it returns error
If the current behavior is a bug, please provide the steps to reproduce.

import mongoose, { Schema, model, UpdateQuery } from 'mongoose';
async function run() {
    mongoose.connect('mongodb://localhost:27017/test');
    const UserModel = model('User', new Schema({
        phone: { type: String, required: true, set: phoneNumberSetter },
        name: String
    }));
}

function phoneNumberSetter(phoneNumber: any, previousValue: any, self: any) {
    let phone: string = phoneNumber;
    if (typeof phoneNumber !== 'string') {
        phone = self.cast(phoneNumber);
    }
    return phone;
}
{
  "compilerOptions": {
    "target": "ES6",                      
    "module": "commonjs",                     
    "strict": true,                          
    "esModuleInterop": true,                 
    "forceConsistentCasingInFileNames": true 
  }
}

What is the expected behavior?
it should not return an error

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
Node.js: 14.17.4 , Mongoose: 6.0.7 , MongoDB: 4.4.3

@vkarpov15
Copy link
Collaborator

What error are you getting? Is there a TypeScript error or runtime error?

@vkarpov15 vkarpov15 added the needs clarification This issue doesn't have enough information to be actionable. Close after 14 days of inactivity label Sep 28, 2021
@GamelSadek GamelSadek changed the title error when using proberty setter error when using propertysetter Sep 28, 2021
@GamelSadek GamelSadek changed the title error when using propertysetter error when using property setter Sep 28, 2021
@GamelSadek
Copy link
Author

GamelSadek commented Sep 28, 2021

What error are you getting? Is there a TypeScript error or runtime error?

TypeScript

Type '{ type: StringConstructor; required: true; set: (phoneNumber: any, previousValue: any, self: any) => string; }' is not assignable to type 'SchemaDefinitionProperty<any> | undefined'.
  Type '{ type: StringConstructor; required: true; set: (phoneNumber: any, previousValue: any, self: any) => string; }' is not assignable to type 'undefined'

@vkarpov15 vkarpov15 added this to the 6.0.12 milestone Oct 1, 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 Oct 1, 2021
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

2 participants