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

Document using virtuals. #23

Closed
marshallswain opened this issue Oct 1, 2015 · 5 comments
Closed

Document using virtuals. #23

marshallswain opened this issue Oct 1, 2015 · 5 comments

Comments

@marshallswain
Copy link
Member

Setting up a virtual currently does nothing to the output This is currently enabled separately in Mongoose using the following commands:

// Create a virtual field for id.
schema.virtual('id').get(function(){
    return this._id.toHexString();
});
// Ensure virtual fields are serialised.
schema.set('toJSON', {virtuals: true});
schema.set('toObject', {virtuals: true});
@ekryski
Copy link
Member

ekryski commented Oct 27, 2015

@marshallswain Again, I think this is more of a documentation issue. If you are working with the mongoose model inside a hook you do have access to the virtuals. If you want virtuals included in your output I think you should have to specify that explicitly like you did above. That's really part of mongoose.

@marshallswain marshallswain changed the title Virtuals don't get serialized. Document serializing virtuals. Oct 28, 2015
@marshallswain marshallswain changed the title Document serializing virtuals. Document using virtuals. Oct 28, 2015
@ekryski
Copy link
Member

ekryski commented Nov 24, 2015

@marshallswain thanks mate!

@germanp
Copy link

germanp commented May 24, 2018

Hi,

The documentation about mongoose's virtuals is not more abailable. What is the suggested way to work virtuals?

I've found that specfiyng lean = false and doing the aforementioned it makes this to work

@loonfly
Copy link

loonfly commented Mar 6, 2020

// Create a virtual field for id.
schema.virtual("id").get(function() {
return this._id.toHexString();
});

schema.set("toJSON", { virtuals: true });
schema.set("toObject", { virtuals: true });

front end is ReactJS with Redux

With this setup, I realize when i service().create() from client to server, the id is in the response. but when service().find() the id is not there, only _id

I did most things accordingly, curious what's wrong or am i missing out anything?

@EmitoSanti
Copy link

// Create a virtual field for id. schema.virtual("id").get(function() { return this._id.toHexString(); });

schema.set("toJSON", { virtuals: true }); schema.set("toObject", { virtuals: true });

front end is ReactJS with Redux

With this setup, I realize when i service().create() from client to server, the id is in the response. but when service().find() the id is not there, only _id

I did most things accordingly, curious what's wrong or am i missing out anything?

Solution ->>>

The problem is the "lean" option in mongoose queries. Feathers.JS has lean = "true" as default (this can be easily seen in the "feathers-mongoose" codebase). More info: https://mongoosejs.com/docs/6.x/docs/api/query.html#query_Query-lean.

solutions:
use a plugin or
in the .service.js of your service set the option -> lean: false in the object

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

5 participants