添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
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 makes a bit more sense if you are using objects like 'Schema.Types.Mixed' inside the Schema definitions. Bit easier to look at than 'mongoose.Schema.Types.Mixed', but maybe a bit less explicit. – Nathan Apr 6, 2018 at 19:46

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;
                As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
– Community
                Dec 4, 2021 at 10:33
        

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.