Trying to add a virtual property on a model and set it’s value as shown below:
const virtualCatName = testSchema.virtual("categoryName");
virtualCatName.get(async function(){
const catByID = await Category.findById(this.categoryId);
console.log(catByID.name);
return catByID.name;
});
The console.log() seems to output the value correctly, however returning the same value doesnt seem to set the virtual property’s value in the response JSON.
The JSON response:
...
...
"id": "666f4807902d3010e5cbcccf",
"categoryName": {},
...
...
what am i missing?