mughil singh
mughil singh

Reputation: 39

NodeJS MongoDB Schema Issue, "ReferenceError: string is not defined"

I am new to nodeJS and am trying to connect a front end form to a mongoDB atlas database. I have an console error "ReferenceError: string is not defined" at my file named "userModel.js".

I have provided other linked files in the projects, although I am not sure if it's necessary to do so.

userModel.js

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

const userSchema = new Schema({
    email: {
        type: string,
        required: true
    },
    password: {
        type: string,
        required: true
    },
    confirmpassword: {
        type: string,
        required: true
    }
  }, {timestamps: true})

  const User = mongoose.model('User', userSchema);
  module.exports = User;

signup.ejs

<form action="/signupRoutes.js" method="post">
     <label for="email">Email:</label>
     <input type="text" id="email" name="email"><br><br>
     <label for="password">Password:</label>
     <input type="text" id="password" name="password"><br><br>
     <input type="submit" value="Submit">
 </form>

server.js

const express = require('express');
const app = express();
const mongoose = require('mongoose');
const ejs = require('ejs');

const homepageRoutes = require('./routes/homepageRoutes');
const signupRoutes = require('./routes/signupRoutes');
const loginRoutes = require('./routes/loginRoutes');

const dotenv = require('dotenv');
dotenv.config();
const dbusername= process.env.DB_USERNAME;
const dbpassword= process.env.DB_PASSWORD;
dbURI = `mongodb+srv://${dbusername}:${dbpassword}@testingcluster.ix9mpsg.mongodb.net/?retryWrites=true&w=majority`;
mongoose.connect(dbURI).then((result) => app.listen(4000)).catch((err) => console.log(err));

app.set('view engine', 'ejs');
app.use(express.static('assets'));

app.use("/index", homepageRoutes);
app.use("/signup", signupRoutes);
app.use("/login", loginRoutes);

signupRoutes.js

const express = require('express');
const router = express.Router();

const User = require('../models/userModel');

router.get('/', (req, res) => {
    res.render('./signup');
    res.end();
});

app.post('/signup', (req, res) => {
    const user = new User({
        email: req.body.email,
        password: req.body.password
    }); 
    user.save();
    res.redirect('/');
});

module.exports = router;

Thanks for your time :)

Sidenote: I am aware that this is not a secure way to store passwords, I am just testing posting to mongoDB for now.

Upvotes: 0

Views: 501

Answers (1)

Jonas Winter
Jonas Winter

Reputation: 31

You have three types in your User Schema (email, password, confirmpassword). You only provide two in the '/signup' route. Delete the confirmpassword type in the User Schema. You don't need to save the password two times just check if its the same.

Upvotes: 1

Related Questions