Biswajeet gope
Biswajeet gope

Reputation: 339

Model.find() is not a function

I am trying to query by finding the postalValue of the model but the error I am getting says there is no method find() .

Below is the error that I am getting when I am querying from the db.

modelInstance.find({postalValue: 123344 }).then(model=> ^ TypeError: modelInstance.find is not a function at Object. (/Users/biswajeet/Documents/webdriverio-test-framework/src/vendor/dataTest.js:44:15) at Module._compile (internal/modules/cjs/loader.js:701:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10) at Module.load (internal/modules/cjs/loader.js:600:32) at tryModuleLoad (internal/modules/cjs/loader.js:539:12) at Function.Module._load (internal/modules/cjs/loader.js:531:3) at Function.Module.runMain (internal/modules/cjs/loader.js:754:12) at startup (internal/bootstrap/node.js:283:19) at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)

const modelInstance = new RegisterModel({
   cred: {
       nameValue: 'Sample',
       emailValue: '[email protected]',
       passwordValue: 'sample122',
   },
   location: {
       addressValue: 'sample address',
       cityValue: 'sample',
       stateValue: 'sample',
       postalValue: 123344,
   },
   card: {
       cardName: 'Sample',
       cardNumber: 231232143,
       securityCode: 131,
       expirationMonth: 1,
       expirationYear: 2022,
   },
})
modelInstance.save(function (err) {
   console.log('@@@@@ Inside the callback ', err);
   if (err) {
       console.log('the error is ', err);
   }
   console.log('saved the model instance @@@@@@');
   console.log(modelInstance, '@@@@@Helllo@@@@@');
});
modelInstance.find({postalValue: 123344 }).then(model=>
   console.log('model@@@',model)
   )

Upvotes: 1

Views: 776

Answers (1)

Ankit Agarwal
Ankit Agarwal

Reputation: 30739

In mongoose you have to do RegisterModel.find() because modelInstance is a instance of RegisterModel and the find() query operates on the model and not the model instance. Same thing applies for findOne and findById.

But for save you use modelInstance.save because you are actually saving the updated data. So, this will also force the mongodb to update the version value of the record i.e, __v if at least one property of that document is modified.

Upvotes: 2

Related Questions