skip to Main Content

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

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:UsersjohnDocumentsreactmern-authenticationmern-auth-servernode_modulesmongooselibschema.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:UsersjohnDocumentsreactmern-authenticationmern-auth-servermern-auth-servernode_modulesmongooselibschema.js:677:13)
    at Schema.add (C:UsersjohnDocumentsreactmern-authenticationmern-auth-servermern-auth-servernode_modulesmongooselibschema.js:728:12)
    at new Schema (C:UsersjohnDocumentsreactmern-authenticationmern-auth-servermern-auth-servernode_modulesmongooselibschema.js:134:10)
    at Object.<anonymous> (C:UsersjohnDocumentsreactmern-authenticationmern-auth-servermodelsuser.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!

2

Answers


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

    resetPasswordLink: {
                type: String,
                default: '',
            },
    
    Login or Signup to reply.
  2. //just this works for me
    const User = require('../models/user'); 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search