Home > Software engineering >  TypeError: Scheme is not a function
TypeError: Scheme is not a function

Time:02-02

I'm trying to do a user registration with a Node js app and MongoDB but I have this error:

var UtenteSchema = Scheme({           
TypeError: Scheme is not a function

There's my model utente.js

const { Mongoose } = require("mongoose");
const mongoose = require("./database");

const Utente = mongoose.model("Utente", new Mongoose.Schema({
    email: String,
    nome: String,
    cognome: String,
    password: String,
    admin: String,
  })
);

module.exports = Utente;

and there's my database.js

var mongoose = require('mongoose');

mongoose.connect("mongodb srv://db:[email protected]/?retryWrites=true&w=majority", {useNewUrlParser: true});

var conn = mongoose.connection;

conn.on('connected', function() {
    console.log('Database connesso');
});
conn.on('disconnected',function(){
    console.log('Database disconnesso');
})
conn.on('Errore', console.error.bind(console, 'Errore di connessione:'));
module.exports = conn;

I'm trying to do a save query to my mongodb atlas online database.

CodePudding user response:

Try the below code snippet

const mongoose = require("mongoose");
const Schema = mongoose.Schema;

const Utente = mongoose.model("Utente", new Schema({
    email: String,
    nome: String,
    cognome: String,
    password: String,
    admin: String,
  })
);

module.exports = Utente;

The difference here is we call .Schema and .model from the default import mongoose

CodePudding user response:

you have exported wrong variable in database.js file

it should be the mongoose instance, e.g:

module.exports = mongoose

What you have is module.exports = conn;. Meaning the connection variable does not have .Schema in it, or is not a function as the log stating

CodePudding user response:

You only need to import mongoose library to create a schema. Your database package is not required to create a schema.

In your utente.js paste the below code.

const mongoose = require("mongoose");

const UtenteSchema = mongoose.Schema(
  {
    email: String,
    nome: String,
    cognome: String,
    password: String,
    admin: String,
  }
);

module.exports = mongoose.model("Utente", UtenteSchema);
  • Related