joseph
joseph

Reputation: 917

Mongoose kitten example not working

I'm trying to get started with the kitten example at the mongoose website, but I keep getting this error saying:

process.nextTick(function() { throw err; }) ^ TypeError: undefined is not a function

My code is directly copied from the example at the website, but it will not work.

var express = require('express');
var mongoose = require('mongoose');

mongoose.connect('mongodb://localhost/db');


var app = express();
var db = mongoose.connection;

db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function (callback) {

  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 " + this.name
    : "I don't have a name"
  console.log(greeting);
}

var Kitten = mongoose.model('Kitten', kittySchema)

var fluffy = new Kitten({ name: 'fluffy' });
fluffy.speak() // "Meow name is fluffy"
});

app.get('/', function(req, res) {
  res.send('hello world');
});

app.get('/secret', function(req, res) {
  res.send('secret page');
});

var port = process.env.PORT || 1337;

app.listen(port, function() {
  console.log('http://127.0.0.1:' + port + '/');

});

I'm sorry if the answer is obvious but I've just started out with Node.js and need some help getting the hang of it.

Thank you in advance.

UPDATE: The full error message

/Users/joseph/Documents/Atom-files/node_modules/mongoose/node_modules/mongodb/lib/server.js:274
      process.nextTick(function() { throw err; })
                                          ^
TypeError: undefined is not a function
    at NativeConnection.<anonymous> (/Users/joseph/Documents/Atom-files/test/app.js:18:17)
    at NativeConnection.g (events.js:199:16)
    at NativeConnection.emit (events.js:104:17)
    at open (/Users/joseph/Documents/Atom-files/node_modules/mongoose/lib/connection.js:485:10)
    at NativeConnection.Connection.onOpen (/Users/joseph/Documents/Atom-files/node_modules/mongoose/lib/connection.js:494:5)
    at /Users/joseph/Documents/Atom-files/node_modules/mongoose/lib/connection.js:453:10
    at /Users/joseph/Documents/Atom-files/node_modules/mongoose/lib/drivers/node-mongodb-native/connection.js:59:5
    at /Users/joseph/Documents/Atom-files/node_modules/mongoose/node_modules/mongodb/lib/db.js:206:5
    at connectHandler (/Users/joseph/Documents/Atom-files/node_modules/mongoose/node_modules/mongodb/lib/server.js:272:7)
    at g (events.js:199:16)
    at emit (events.js:107:17)
    at /Users/joseph/Documents/Atom-files/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:374:23
    at /Users/joseph/Documents/Atom-files/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:730:13
    at Callbacks.emit (/Users/joseph/Documents/Atom-files/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:84:3)
    at null.messageHandler (/Users/joseph/Documents/Atom-files/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:219:23)
    at Socket.<anonymous> (/Users/joseph/Documents/Atom-files/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/connection/connection.js:259:22)
14 May 21:02:09 - [nodemon] app crashed - waiting for file changes before starting...

Upvotes: 2

Views: 1495

Answers (2)

A.kumar
A.kumar

Reputation: 9

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test', {useNewUrlParser: true});

var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
  // we're connected!`enter code here`
  var kittySchema = new mongoose.Schema({
    name: String
  });

kittySchema.methods.speak = function () {
    var greeting = this.name ? "Meow name is " + this.name : "I don't have a name";
    console.log(greeting);
  }

  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"

});

Upvotes: 0

Andrew Lavers
Andrew Lavers

Reputation: 8141

You're compiling your schema twice, and one of those times is before defining the method. This line specifically:

var Kitten = mongoose.model('Kitten', kittySchema)

You need to wait until after you've defined your kittySchema.methods before compiling it with mongoose.model. Your own code comment is telling you not to do this :)

Also, for future reference, the TypeError: undefined is not a function error you pasted isn't of much use but the call stack below it is, where it points out that fluffy has no method 'speak'.

Upvotes: 6

Related Questions