Home > database >  TypeError: Nodejs.getName is not a function
TypeError: Nodejs.getName is not a function

Time:09-22

I try to use instance methods of schema But fail It show , not function can someone explain me

const bookSchema =new schema(
  {
  name : String ,
  Author : String
  }
);

const Model = mongoose.model;
const bookModel = Model('book',bookSchema);


bookSchema.methods.getName = function(){
 return ('the name of book is '   this.name   'the name of book author is '   this.Author);
}

const Nodejs =new bookModel({name:'Node.js Book' , Author:'Me '});
const Authorname = Nodejs.getName();
console.log(AuthorName);

CodePudding user response:

const bookSchema = new schema(
  {
   name : String ,
   Author : String
  }
);

const bookModel = mongoose.model('Book',bookSchema);

let GreenTrees = new bookModel({ name: 'GoodBook', Author: 'Frogs' });

console.log(`the name of book is ${GreenTrees.get(name)} the name of book 
author is ${GreenTrees.get(Author)}`);


let yourBook = new bookModel({name:'Node.js Book' , Author:'Me '});
let Authorname = yourBook.get(Author);
console.log(AuthorName);
  • Related