-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
Comments
Definitely a feature we need to add, thanks for the suggestion. Also, to get rid of the warning add |
In order to be more consistent with the API for #5704, defaults, etc. we'll instead support setting 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. |
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? |
@p3x-robot I don't understand your question, can you please clarify with some code samples? |
@vkarpov15 hello! how are you? 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 |
@p3x-robot I opened up a new issue to track this ☝️ |
I love the "refPath" instead of "ref" for dynamic population. This works great.
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.
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, useopenUri()
instead, or set theuseMongoClient
option if usingconnect()
orcreateConnection()
.) when trying to use the latest version, so I have to roll back.The text was updated successfully, but these errors were encountered: