How to call schema method inside another method in the same model using mongoose

Viewed 49

I have a model named "Notification" and it has two methods. I want to call a method inside another method and query the same model using mongoose.

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

const NotificationSchema = new Schema({
    code: { type: 'string', required: true, unique: true },
    name: { type: 'string', required: true }
}, collection : "notification");

NotificationSchema.methods.MethodA = async () => {
   // querying the same model
   let query = await this.find({"code" : "abc"}).lean();
   this.MethodB(); 
};

NotificationSchema.methods.MethodB = () => {
   console.log("this is methodB");
};

module.exports = mongoose.model("Notification", NotificationSchema);

Now, can't query the same model and calling methodB in methodA is throwing an error

this.methodB is not a function
0 Answers
Related