Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I fix Invalid Schema Configuration in Mongoose and React?

I am trying to create a basic authentication with ReactJS. I have created a model with mongoose but there seems to be an issue importing/accessing the model in my controller code.

/models/user.js

const mongoose = require('mongoose')
const crypto = require('crypto')

// user schema
const userSchema = new mongoose.Schema({
  name: {
    type: String,
    trim: true,
    required: true,
    max: 32
  },
  email: {
    type: String,
    trim: true,
    required: true,
    unique: true,
    lowercase: true
  },
  hashed_password: {
    type: String,
    required: true
  },
  salt: String,
  role: {
    type: String,
    default: 'subscriber'
  },
  resetPasswordLink: {
    data: String,
    default: ''
  },
}, {timestamps: true})

// virtual
userSchema.virtual('password')
.set(function(password) {
  // code
})
.get(function() {
  // code
})

// methods
userSchema.methods = {
    // code
  },

  makeSalt: function() {
    // code
  }
};

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

/controllers/auth.js

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

const User = require('../models/user').schema; // import the user model

exports.signup = (req, res) => {
  const{name, email, password} = req.body
  
  User.findOne({email: email}).exec((err, user) => {
    if (user) {
      return res.status(400).json({
        error: 'Email is taken'
      })
    }
  })

  let newUser = new User({name, email, password})

  newUser.save((err, success) => {
    if (err) {
      console.log('SIGNUP ERROR', err)

      return res.status(400).json({
        error: err
      })
    }

    res.json({
      message: 'Signup sucess! Please signin'
    })
  })
}

Problem: When I run npm start in the terminal, I see this error: throw new TypeError(Invalid schema configuration: \${val}` is not ` +

C:\Users\john\Documents\react\mern-authentication\mern-auth-server\node_modules\mongoose\lib\schema.js:677
      throw new TypeError(`Invalid schema configuration: \`${val}\` is not ` +
      ^

TypeError: Invalid schema configuration: `` is not a valid type at path `default`. See mongoose-schematypes for a list of valid schema types.
    at Schema.add (C:\Users\john\Documents\react\mern-authentication\mern-auth-server\mern-auth-server\node_modules\mongoose\lib\schema.js:677:13)
    at Schema.add (C:\Users\john\Documents\react\mern-authentication\mern-auth-server\mern-auth-server\node_modules\mongoose\lib\schema.js:728:12)
    at new Schema (C:\Users\john\Documents\react\mern-authentication\mern-auth-server\mern-auth-server\node_modules\mongoose\lib\schema.js:134:10)
    at Object.<anonymous> (C:\Users\john\Documents\react\mern-authentication\mern-auth-server\models\user.js:5:20)
    at Module._compile (node:internal/modules/cjs/loader:1254:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)
    at Module.load (node:internal/modules/cjs/loader:1117:32)
    at Module._load (node:internal/modules/cjs/loader:958:12)
    at Module.require (node:internal/modules/cjs/loader:1141:19)
    at require (node:internal/modules/cjs/helpers:110:18)

Do you guys know what can fix the terminal error? Any help is greatly appreciated. Thanks!

>Solution :

you have a type error in resetPasswordLink.
instead of data: String change to type: String as shown below.

resetPasswordLink: {
            type: String,
            default: '',
        },
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading