javascript - Mongoose kitten example not working -
i'm trying started kitten example @ mongoose website, keep getting error saying:
process.nexttick(function() { throw err; }) ^ typeerror: undefined not function
my code directly copied example @ website, 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 added schema before compiling mongoose.model() kittyschema.methods.speak = function () { var greeting = this.name ? "meow name " + this.name : "i don't have name" console.log(greeting); } var kitten = mongoose.model('kitten', kittyschema) var fluffy = new kitten({ name: 'fluffy' }); fluffy.speak() // "meow name 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 answer obvious i've started out node.js , need getting hang of it.
thank in advance.
update: 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 not function @ nativeconnection.<anonymous> (/users/joseph/documents/atom-files/test/app.js:18:17) @ nativeconnection.g (events.js:199:16) @ nativeconnection.emit (events.js:104:17) @ open (/users/joseph/documents/atom-files/node_modules/mongoose/lib/connection.js:485:10) @ nativeconnection.connection.onopen (/users/joseph/documents/atom-files/node_modules/mongoose/lib/connection.js:494:5) @ /users/joseph/documents/atom-files/node_modules/mongoose/lib/connection.js:453:10 @ /users/joseph/documents/atom-files/node_modules/mongoose/lib/drivers/node-mongodb-native/connection.js:59:5 @ /users/joseph/documents/atom-files/node_modules/mongoose/node_modules/mongodb/lib/db.js:206:5 @ connecthandler (/users/joseph/documents/atom-files/node_modules/mongoose/node_modules/mongodb/lib/server.js:272:7) @ g (events.js:199:16) @ emit (events.js:107:17) @ /users/joseph/documents/atom-files/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:374:23 @ /users/joseph/documents/atom-files/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:730:13 @ callbacks.emit (/users/joseph/documents/atom-files/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:84:3) @ null.messagehandler (/users/joseph/documents/atom-files/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:219:23) @ 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 file changes before starting...
you're compiling schema twice, , 1 of times before defining method. line specifically:
var kitten = mongoose.model('kitten', kittyschema)
you need wait until after you've defined kittyschema.methods
before compiling mongoose.model
. own code comment telling not :)
also, future reference, typeerror: undefined not function
error pasted isn't of use call stack below is, points out fluffy has no method 'speak'
.
Comments
Post a Comment