A speak method was created for Schema. Why can’t subsequent code call this speak method?

  node.js, question
// getting-started.js
 var mongoose = require('mongoose');
 mongoose.connect('mongodb://localhost/test');
 
 var db = mongoose.connection;
 db.on('error', console.error.bind(console, 'connection error:'));
 db.once('open', function() {
 // we're connected!
 });
 
 var kittySchema = mongoose.Schema({
 name: String
 });
 
 var Kitten = mongoose.model('Kitten', kittySchema);
 
 var silence = new Kitten({ name: 'Silence' });
 console.log(silence.name);  // 'Silence'
 
 // NOTE: methods must be added to the schema before compiling it with mongoose.model()
 kittySchema.methods.speak = function () {
 var greeting = this.name
 ?  "Meow name is" plus this.name
 : "I don't have a name";
 console.log(greeting);
 bracket
 
 var Kitten = mongoose.model('Kitten', kittySchema);
 
 var fluffy = new Kitten({ name: 'fluffy' });
 fluffy.speak();  // "Meow name is fluffy"
 
 fluffy.save(function (err, fluffy) {
 if (err) return console.error(err);
 fluffy.speak();
 });
 
 Kitten.find(function (err, kittens) {
 if (err) return console.error(err);
 console.log(kittens);
 })
 
 Kitten.find({ name: /^fluff/ }, callback);

The error is as follows:
图片描述

// NOTE: methods must be added to the schema before compiling it with mongoose.model()
kittySchema.methods.speak = function () {
var greeting = this.name

?  "Meow name is" plus this.name
 : "I don't have a name";

console.log(greeting);
bracket

var Kitten = mongoose.model(‘Kitten’, kittySchema);

The manual is very clear. To uninstall before you create this model.

Change to read as follows:

// getting-started.js
var mongoose = require(‘mongoose’);
mongoose.connect(‘mongodb://localhost/test’);

var db = mongoose.connection;
db.on(‘error’, console.error.bind(console, ‘connection error:’));
db.once(‘open’, function() {
// we’re connected!
});

var kittySchema = mongoose.Schema({
 name: String
 });
 
 // NOTE: methods must be added to the schema before compiling it with mongoose.model()
 kittySchema.methods.speak = function () {
 var greeting = this.name
 ?  "Meow name is" plus this.name
 : "I don't have a name";
 console.log(greeting);
 bracket

var Kitten = mongoose.model(‘Kitten’, kittySchema);

var silence = new Kitten({ name: ‘Silence’ });
console.log(silence.name); // ‘Silence’

var fluffy = new Kitten({ name: ‘fluffy’ });
fluffy.speak(); // “Meow name is fluffy”

fluffy.save(function (err, fluffy) {

if (err) return console.error(err);
 fluffy.speak();

});

Kitten.find(function (err, kittens) {

if (err) return console.error(err);
 console.log(kittens);

})

Kitten.find({ name: /^fluff/ }, callback);