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

Getters not applied correctly to arrays #30

Closed
beltingtt opened this issue Sep 18, 2023 · 1 comment
Closed

Getters not applied correctly to arrays #30

beltingtt opened this issue Sep 18, 2023 · 1 comment
Labels
bug Something isn't working

Comments

@beltingtt
Copy link

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

bug

What is the current behavior?

Getters are not being applied correctly to arrays in lean queries. This seems to be have been broken with the release of mongoose@7.5.0 as it is working as expected in mongoose@7.4.5.

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

import mongoose, { Schema } from "mongoose";
import mongooseLeanGetters from "mongoose-lean-getters";

function upper(value) {
    return value.toUpperCase();
}

const userSchema = new Schema({
    name: {
        type: String,
        get: upper,
    },
    emails: [
        {
            type: String,
            get: upper,
        },
    ],
});

userSchema.plugin(mongooseLeanGetters);

const User = mongoose.model("User", userSchema);

const run = async () => {
    await mongoose.connect("mongodb://localhost:27017/test");

    const user = new User({
        name: "one",
        emails: ["two", "three"],
    });
    await user.save();

    const foundUser = await User.findById(user._id, {
        _id: 0,
        name: 1,
        emails: 1,
    }).lean({ getters: true });

    console.log(foundUser); // { name: 'ONE', emails: [ 'two', 'three' ] }
};

run();

Please notice that the upper getter is not being applied to the emails array field:

{ name: 'ONE', emails: [ 'two', 'three' ] }

What is the expected behavior?

The upper getter is applied to the emails array field and the above code logs the following:

{ name: 'ONE', emails: [ 'TWO', 'THREE' ] }

What are the versions of Node.js, mongoose-lean-getters, and Mongoose are you are using? Note that "latest" is not a version.

node: 16.20.2
mongoose: 7.5.2
mongoose-lean-getters: 1.1.0

@IslandRhythms IslandRhythms added the bug Something isn't working label Sep 26, 2023
@shawnmcknight
Copy link

Running into this as well. I suspect it was caused by the change to getters on arrays in Automattic/mongoose#13774 which was part of 7.5.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants