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.
3
Answers
Try the below code snippet
The difference here is we call
.Schema
and.model
from the default importmongoose
you have exported wrong variable in
database.js
fileit should be the mongoose instance, e.g:
module.exports = mongoose
What you have is
module.exports = conn;
. Meaning theconnection
variable does not have.Schema
in it, or is not a function as the log statingYou only need to import
mongoose
library to create a schema. Yourdatabase
package is not required to create a schema.In your utente.js paste the below code.