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

AutoPopulate on type.string #119

Closed
Bartas139 opened this issue Apr 28, 2024 · 1 comment
Closed

AutoPopulate on type.string #119

Bartas139 opened this issue Apr 28, 2024 · 1 comment

Comments

@Bartas139
Copy link

Hi, I have schema code like this:

const Role = mongoose.model('Role', roleModel.schema)
const userSchema = mongoose.Schema(
    {
        _id: {
            type: String,
            default: randomUUID
        },
        email: {
            type: String,
            unique: true,
            required: [true, 'email is required']
        },
        password: {
            type: String,
            required: [true, 'password is required']
        },
        roles: [{
            type: mongoose.Schema.Types.String,
            ref: 'Role',
            autopopulate: true
        }]
    },
    {
        timestamps: true
    }
)

userSchema.plugin(require('mongoose-autopopulate'))

And the autopopulate does not work at all. There is no error, but the findOne niether find, or findMany is not populated. I always get just the Array of roles IDs. Is there somethind wrong with my code?

@Bartas139
Copy link
Author

Ok... wierd thing I also use

userSchema.methods.toJSON = function() { const {password, ...rest} = {...this._doc} return rest; }

And for some reason the userSchema.plugin(require('mongoose-autopopulate')) muset be above the methods... Quite dont know why, but now it works

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