Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialRichard Brown
6,950 PointsTypeError: Schema is not a constructor
Hi all.
I wonder if someone can help me, please. :)
Here's my code:
'use strict'
var mongoose = require("mongoose");
mongoose.connect("mongodb://localhost:27017/sandbox");
var db = mongoose.connection;
db.on("error", function(error){
console.log("connection error:", err);
});
db.once("open", function(){
console.log("database connection successful");
// all database communication goes here
var Schema = new mongoose.Schema;
var AnimalSchema = new Schema({
type: String,
color: String,
size: String,
mass: Number,
name: String
});
var Animal = mongoose.model("Animal", AnimalSchema);
var elephant = new Animal({
type: "elephant",
size: "big",
color: "grey",
mass: 6000,
name: "Lawerence"
});
elephant.save(function(err){
if(err) console.log("saved failed.");
else console.log("saved");
db.close(function(){
console.log("connection closed.");
});
});
});
And here's the console:
$ node mongoose_sandbox.js
database connection successful
TypeError: Schema is not a constructor
at NativeConnection.<anonymous> (C:\TreeHouse\rest-api\mongoose_sandbox.js:17:24)
at Object.onceWrapper (events.js:313:30)
at emitNone (events.js:106:13)
at NativeConnection.emit (events.js:208:7)
at open (C:\TreeHouse\rest-api\node_modules\mongoose\lib\connection.js:512:11)
at NativeConnection.Connection.onOpen (C:\TreeHouse\rest-api\node_modules\mongoose\lib\connection.js:521:5)
at C:\TreeHouse\rest-api\node_modules\mongoose\lib\connection.js:483:11
at C:\TreeHouse\rest-api\node_modules\mongoose\lib\drivers\node-mongodb-native\connection.js:60:5
at C:\TreeHouse\rest-api\node_modules\mongodb\lib\db.js:234:5
at Server.connectHandler (C:\TreeHouse\rest-api\node_modules\mongodb\lib\server.js:306:7)
at Object.onceWrapper (events.js:315:30)
at emitOne (events.js:116:13)
at Server.emit (events.js:211:7)
at C:\TreeHouse\rest-api\node_modules\mongodb-core\lib\topologies\server.js:572:23
at C:\TreeHouse\rest-api\node_modules\mongodb-core\lib\topologies\server.js:492:18
at ScramSHA1.reauthenticate (C:\TreeHouse\rest-api\node_modules\mongodb-core\lib\auth\scram.js:312:25)
C:\TreeHouse\rest-api\node_modules\mongodb\lib\server.js:309
process.nextTick(function() { throw err; })
Any suggestions?
Thanks in advance.
2 Answers
Rich Donnellan
Treehouse Moderator 27,708 PointsCheck this line:
var Schema = new mongoose.Schema;
Removing the new
should do the trick.
Richard Brown
6,950 PointsThank you so much for your prompt reply.. A feel a 'doh!' coming on! :)
Rich Donnellan
Treehouse Moderator 27,708 PointsNo problem, Richard! Get used to "doh" moments!