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:
Any idea what I am doing wrong?
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.
you forgot to define the Schema like this on you second line
var Schema = mongoose.Schema
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With