Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
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.
–
This can be occurred due to several reasons. first one is that you might forgotten to import Schema.You can fix that as follows.
const Schema = mongoose.Schema;
const serialSchema = new Schema({
serial: {type: String},
game: {type: String},
date: {type: Date, default: Date.now},
Sometimes you have forgotten to import your newly created model.This type of errors can be solve by importing created model to your working file.
const serialModel = mongoose.model('serials', serialSchema);
In you case call as mongoose.Schema
as you did not defined it as
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.