Mongoose custom virtual field return promise

Viewed 155

I'm new in mongoDB, I have virtual field like this

videoSchema.virtual('playURL').get(async function () {
    return await getVideoURLbyId(this.video_id, true)
        .then(res => res)
})

videoSchema.set('toObject', { virtuals: true })
videoSchema.set('toJSON', { virtuals: true })

I can log the result but when show on browser, it shows empty result playURL:{}. How to fix it?

Thank you

1 Answers

Not possible use asynchronous in Virtual look this issue, because virtuals are just getters/setters, which are assumed to be synchronous. Use a custom method instead

Related