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

[SERIOUS] Setters/ Getters not working, docs fail instanceof mongoose.Document check #50

Open
vresetnikov opened this issue Nov 26, 2022 · 3 comments
Labels
question Further information is requested

Comments

@vresetnikov
Copy link

vresetnikov commented Nov 26, 2022

Hello,

We have been developing a system for money operations where Decimal128 field values need to be converted to string to appear correctly in JSON. After running tests trying to isolate the problem, it has become apparent that mongoose-aggregate-paginate-v2 does not seem to be able to use virtuals, namely the mongoose's native set and get functionality as the document Decimal128 fields do not get converted and instead appear in Json as such:

"totalValue": {
      "$numberDecimal": "2978.60"
  },

Notice the preceding $numberDecimal.

Additionally, running the following check: results.docs.map(result => result instanceof mongoose.Document) will fail and return false, which might be related to why the getters fail. Doesn't this library return the aggregated documents?

This is a very urgent and pressing issue for us, we would be immensely grateful if a reply/ suggestion/ solution could be provided.

Thank you very much

@vresetnikov
Copy link
Author

After reading carefully the docs on Aggregate I think I have come to understand that aggregate returns a plain JS object and therefore no Document related methods can be called on it and this has nothing to do with the library. Any suggestions on how could the desired result be achieved without abandoning the aggregation?

@aravindnc
Copy link
Owner

@vresetnikov Based on my understanding, getters/setters are registered at the schema level, so aggregate output will not have access to model instance.

Let's see if anybody have other methods to overcome this.

@aravindnc aravindnc added the question Further information is requested label Nov 30, 2022
@oscar-v0
Copy link

Not sure if this solves your issue but this worked for me.
The solution is to instantiate a new Document instance by passing in the plain JS object as parameter, e.g:

const users = await UserModel.aggregatePaginate(...);
users.docs = users.docs.map(doc => new UserModel(doc));

// This now works
await UserModel.populate(users.docs, "messages");

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants