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

Mongoose Virtuals do not work #41

Open
EricHier opened this issue Dec 18, 2020 · 4 comments
Open

Mongoose Virtuals do not work #41

EricHier opened this issue Dec 18, 2020 · 4 comments

Comments

@EricHier
Copy link

As shown in this example, mongoose supports virtual (computed) properties on objects. I've implemented an example resource (just like the first one in the mongoose docs) and I am not able to show the property in the frontend.

What do I miss? Are virtuals supported in admin-bro-mongoose?

@Mannhattan
Copy link

Mannhattan commented Feb 6, 2021

I struggled to get them working too for quite some time now and I found a way to do it (or at least display them in adminbro). You have to add a mongoose middleware for it to automatically populate your virtual field before they get to adminbro. I think simplest answer would be to do this:

Mongoose schema definition:

const schema = new mongoose.Schema({
    name: {
        type: String,
        required: false
    },
}, {
    timestamps: true,
    toJSON: { virtuals: true },
    toObject: { virtuals: true }
});

schema.virtual('VIRTUAL_FIELD', {
    ref: "...",
    localField: "...",
    foreignField: '...',
});

// here you populate your virtual field before it gets returned to adminbro (find query)
schema.pre('find', function () {
    this.populate('VIRTUAL_FIELD');
});

module.exports = mongoose.model('schema', schema);

@EricHier
Copy link
Author

Thank you for your solution! :) Don't you think that adminbro-mongoose should populate the fields automatically? This would be expected behaviour from my view. This would also allow you to only populate the field when using adminbro and not on every query on the schema.

@Romuloalmeida
Copy link

Romuloalmeida commented Apr 13, 2021

It didn't work for me. I'm a little confused now.
Actually I'm beginner with MongoDB so my question is: Does virtual really need to set foreignField and localFields ? As I know, virtuals are defined as:

const virtual = AffiliateSchema.virtual('fullname');
virtual.get(function(value, virtual, doc) {
  return "Test"
});

Just for viewing purposes, of couse.

@zdraganov
Copy link

I can confirm they are not working. Also what I see is that if the _id field is not of type ObjectId the adminjs do not properly builds the link in the relation.
Like this:

// On one side
champion: { type: String, required: true, ref: 'Champions' },

// On the other side
_id: {
  type: String,
  required: true,
  unique: true,
}

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

4 participants