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

"refPath" doesn't work for virtuals? #5602

Closed
miliu99 opened this issue Aug 30, 2017 · 6 comments
Closed

"refPath" doesn't work for virtuals? #5602

miliu99 opened this issue Aug 30, 2017 · 6 comments
Labels
new feature This change adds new functionality, like a new method or class
Milestone

Comments

@miliu99
Copy link

miliu99 commented Aug 30, 2017

I love the "refPath" instead of "ref" for dynamic population. This works great.

    var activitySchema = new Schema({
        refId: {type: Schema.ObjectId, refPath: 'refKind'},
        refKind: String
    };

However, I don't like to share the same field "refId" for either id or the object. So, I found a great solution using virtuals. However, to my dismal, the "refPath" doesn't seem to work for virtuals, although "ref" works.

var activitySchema = new Schema({
      refId: {type: Schema.ObjectId},
      refKind: String
  }, { _id: false, toObject: {virtuals: true}, toJSON: { virtuals: true }});
  activitySchema.virtual('refObj', {refPath: 'refKind', localField: 'refId', foreignField: '_id', justOne: true});

I understand that nothing is mentioned in the document about using refPath in virtuals, so I'm not sure if this is just not supported or a bug. Here is something I tried that doesn't work. Any clue?

Oh, I'm using v4.10.8. Not sure if it is working in 4.11.9 because I run into other problems (DeprecationWarning: open() is deprecated in mongoose >= 4.11.0, use openUri() instead, or set the useMongoClient option if using connect() or createConnection().) when trying to use the latest version, so I have to roll back.

@vkarpov15 vkarpov15 added this to the 4.13 milestone Sep 4, 2017
@vkarpov15 vkarpov15 added the new feature This change adds new functionality, like a new method or class label Sep 4, 2017
@vkarpov15
Copy link
Collaborator

Definitely a feature we need to add, thanks for the suggestion.

Also, to get rid of the warning add useMongoClient: true, like mongoose.connect(MONGO_URI, { useMongoClient: true })

@vkarpov15
Copy link
Collaborator

In order to be more consistent with the API for #5704, defaults, etc. we'll instead support setting ref to a function in your virtual with the doc as the context. So instead of:

  activitySchema.virtual('refObj', {refPath: 'refKind', localField: 'refId', foreignField: '_id'});

You will have to do

activitySchema.virtual('refObj', {ref: function() { return this.refKind; }, localField: 'refId', foreignField: '_id'});

Should make for a cleaner API, we'll consider doing this for regular refPath as well.

Fonger added a commit to Fonger/mongoose that referenced this issue Aug 13, 2018
Fonger added a commit to Fonger/mongoose that referenced this issue Aug 13, 2018
@Fonger Fonger mentioned this issue Aug 13, 2018
@p3x-robot
Copy link
Contributor

how come the refObj in above that works instead of giving an object it is an array, even though it can match 1 unique doc, how come?

@vkarpov15
Copy link
Collaborator

@p3x-robot I don't understand your question, can you please clarify with some code samples?

@p3x-robot
Copy link
Contributor

@vkarpov15 hello! how are you?
so refPath virtual is not working:

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const _ = require('lodash');

const PdfFile = new Schema({
    docType: {
        type: String,
        index: true,
    },
    docId: {
        type: Schema.ObjectId,
        index: true,
        refPath: 'docTypeModelName'
    },
    buffer: {
        type: Buffer,
    }
},{
    timestamps: true
});

// not working
PdfFile.virtual('docTypeModelName').get(function () {
    const docTypeModelName = _.pascalCase(this.docType);
    console.log('docTypeModelName', docTypeModelName)
    return docTypeModelName
});

// working
PdfFile.virtual('doc', {
    ref: function() {
        const ref = _.pascalCase(this.docType)
        console.warn('PdfFile.virtual ref', ref)
        return ref;
    },
    localField: 'docId',
    foreignField: '_id'
});

module.exports = PdfFile;

How come with using refPath as virtual is not working?
It took me 2 days to find as a virtual with ref.

@vkarpov15
Copy link
Collaborator

@p3x-robot I opened up a new issue to track this ☝️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new feature This change adds new functionality, like a new method or class
Projects
None yet
Development

No branches or pull requests

4 participants