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 are not executed automatically with Express' res.json() #13049

Closed
2 tasks done
imageck opened this issue Feb 18, 2023 · 3 comments · Fixed by #13058
Closed
2 tasks done

Getters are not executed automatically with Express' res.json() #13049

imageck opened this issue Feb 18, 2023 · 3 comments · Fixed by #13058
Labels
docs This issue is due to a mistake or omission in the mongoosejs.com documentation
Milestone

Comments

@imageck
Copy link

imageck commented Feb 18, 2023

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

6.9.0

Node.js version

18.13.0

MongoDB server version

5.0.14

Typescript version (if applicable)

No response

Description

The documentation mentions:

By default, Mongoose executes getters when converting a document to JSON, including Express' res.json() function.

However, it doesn't seem to be the case with find() (not sure about other methods).

Steps to Reproduce

I have some numeric keys in my schema, e.g.:

  num1: {
    type: Schema.Types.Decimal128,
    get: n => n.toString()
  },

and my controller function:

async function getCollectionItems(req, res) {
  let { id } = req.params;
  try {
    let items = await Item.find()
      .where('collectionId').equals(id)
      .exec();
    res.status(200).json(items);
  } catch(err) {
    res.sendStatus(500);
  }

On the front-end, a nested object is received:

[{... ,"num1":{"$numberDecimal":"123.45"}, ...}]

Expected Behavior

One would expect the getter is executed at the time of conversion to JSON but for some reason it's not, unless the toJSON.getters option is explicitly set to true:

  const Decimal = mongoose.model("Decimal", new mongoose.Schema({
    num1: {
      type: mongoose.Schema.Types.Decimal128,
      get: n => n.toString()
    },  
  }, { toJSON: { getters: true } }));

  const dec1 = await Decimal.create({
    num1: 123.45,
  }); 
  let decimals = await Decimal.find().exec();
  console.log(JSON.stringify(decimals));
// [{"_id":"63f10eb9a1a7e4ed6174dd61","num1":"123.45","__v":0,"id":"63f10eb9a1a7e4ed6174dd61"}]

So it's one of the three:

  1. I'm doing something wrong;
  2. The documentation is misleading;
  3. There is a bug;
@imageck
Copy link
Author

imageck commented Feb 18, 2023

Did another test on the real project and yep, I can confidently say that I'm not doing something wrong. Explicitly setting getters: true works. So it's one of the last two 😛

@vkarpov15
Copy link
Collaborator

You're right, this is an issue in our docs. I'm sorry for the trouble, and thanks for pointing this out. You can work around this with mongoose.set('toJSON', { getters: true }).

@imageck
Copy link
Author

imageck commented Feb 20, 2023

Oh, there was no trouble, just slight confusion.

Thank you for looking into this.

@hasezoey hasezoey added the docs This issue is due to a mistake or omission in the mongoosejs.com documentation label Feb 21, 2023
@vkarpov15 vkarpov15 added this to the 6.9.3 milestone Feb 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs This issue is due to a mistake or omission in the mongoosejs.com documentation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants