Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoose and new Schema: returns "ReferenceError: Schema is not defined"

I am creating a new sample application, where I try to connect to a MongoDB database through Mongoose.

I create a new schema in my service.js file, but I get the following error when I run nodemon app.js: "ReferenceError: Schema is not defined"

App.js code:

var http = require('http');
var express = require('express');
var serials = require('./service');
var app = express();
var mongoose = require('mongoose');


var port = 4000;
app.listen(port);

mongoose.connect('mongodb://localhost:27017/serialnumbers')

app.get('/api/serials',function(req,res){
    serials.getSerial(req, res, function(err, data) {
        res.send(data);
    });
});

Service.js code:

var mongoose = require('mongoose');

var serialSchema = new Schema({
    serial: {type: String},
    game: {type: String},
    date: {type: Date, default: Date.now},
});
mongoose.model('serials', serialSchema);

exports.getSerial = function(req,res,cb) {
    mongoose.model('serials').find(function(err,data) {
        cb(err,data);
    });
};

I saw an answer here on StackOverflow that referenced it could be the version of Mongoose. But npm list gives me this:

enter image description here

Any idea what I am doing wrong?

like image 950
Lars Holdgaard Avatar asked Jul 23 '14 10:07

Lars Holdgaard


2 Answers

Exactly, in your Service.js, what is Schema? You don't have an object named Schema.

...
var serialSchema = new Schema({
                       ^^^^^^

change it to mongoose.Schema then it will be fine.

like image 113
anvarik Avatar answered Oct 14 '22 02:10

anvarik


you forgot to define the Schema like this on you second line

var Schema = mongoose.Schema

like image 7
Tere Villalba Avatar answered Oct 14 '22 03:10

Tere Villalba