JavaScript Cannot overwrite `users` model once compiled node js cannot overwrite user model once compiled

I am getting this error while compiling my code

throw new mongoose.Error.OverwriteModelError(name);
      ^
OverwriteModelError: Cannot overwrite `users` model once compiled.

This is my Model/users.js file

const mongoose = require('mongoose');
const bcrypt = require('bcryptjs');
const config = require('../config/database');

//User Schema
const UserSchema = mongoose.Schema({
  name: {
    type:String
  },
  email: {
    type:String,
    required:true
  },
  username: {
    type:String,
    required:true
  },
  password: {
    type:String,
    required:true
  }
});

const users = module.exports = mongoose.model('users',UserSchema);

module.exports.getUserById = function(id,callback)
{
  User.findById(id,callback);
}

module.exports.getUserByUsername = function(username,callback){
  const query = {username: username}
  user.findOne(query,callback);
}
module.exports.addUser = function(newUser,callback){
  bcrypt.genSalt(10,(err,salt)=>{
    if(err)
    {
      throw err;
    }
    bcrypt.hash(newUser.password,salt,(err,hash)=>{
      newUser.password=hash;
      newUser.save(callback);
    });
  });
}

This is my route/users.js file

const express = require('express');
const router = express.Router();
const passport = require('passport');
const jwt = require('jsonwebtoken');
var Log = require('log')
  , log = new Log('info');

const User = require('../Model/users');
//const ap = express();

//Register router
router.post('/register',(req,res,next)=>{
  log.info('entering register');
  let newUser = new User({
    name:req.body.name,
    email:req.body.email,
    username:req.body.username,
    password:req.body.password
  });
  User.addUser(newUser,(err,user)=>{
    log.info("inside add user");
    if(err){
      log.error('error while registaring ' + err);
      res.json({success:false,msg:'failed to register user'});
    }
    else {
      res.json({sucess:true,msg:'user registered succefully'});
    }
  })
});
router.get('/authenticate',(req,res,next)=>{
  res.send("authenticated");
});
router.get('/profile',(req,res,next)=>{
  res.send('profile');
});
module.exports=router;

passport.js

const JwtStrategy = require('passport-jwt').Strategy;
const ExtractJwt = require('passport-jwt').ExtractJwt;
const User = require('../model/users');
const config = require('../config/database');

module.exports = function(passport){
  let opts = {};
  opts.jwtFromRequest = ExtractJwt.fromAuthHeader();
  opts.secretOrKey = config.secret;
  passport.use(new JwtStrategy(opts,(jwt_payload,done)=>{
    User.getUserById(jwt_payload._id,(err,user)=>{
      if(err)
      {
        return done(err,false);
      }
      if(user){
        return done(null,user);
      }
      else {
        return done(null,false);
      }
    })
  }));
}

I have tried suggestion from [Cannot overwrite model once compiled Mongoose but I am not able to understand what the problem is.

Answer:1

The problem is you are compiling User two times in the runtime
once you do users = mongoose.model('users',UserSchema); mongo will create the collection users so i think you are doing it again somewhere in the project

Or if you are working with fawn maybe you are using it more than onetime

Answer:2

Design your Schema with some options

//User Schema
const UserSchema = mongoose.Schema({
  name: {
    type:String
  },
  email: {
    type:String,
    required:true
  },
  username: {
    type:String,
    required:true
  },
  password: {
    type:String,
    required:true
  }
}, {strict: false});

const users = module.exports = mongoose.model('users',UserSchema);

{strict: false} is for future! How? Now your schema has 10 fields, if in future you want to add an object with 12 (anything more than 10), you can do it, because there is no strictness in inserting objects with these 10 fields. Even if less fields.

May be this may solve your issue

Answer:3

I need to do something like below- function GetParentID() { var tv = $('#treeview').data('kendoTreeView'), selected = tv.select(), item = tv.dataItem(selected); var Value= (item.ID); ...

I need to do something like below- function GetParentID() { var tv = $('#treeview').data('kendoTreeView'), selected = tv.select(), item = tv.dataItem(selected); var Value= (item.ID); ...

  1. pass return value to callback javascript
  2. pass return value another function python
  3. pass return value another function javascript
  4. pass return value from one function to another
  5. pass return value from one function to another in javascript
  6. pass return value another function java
  7. pass return value
  8. pass return value in python
  9. pass return value javascript
  10. pass return value bash
  11. c++ pass return value by reference
  12. how to pass return value in java
  13. batch pass return value
  14. ruby pass return value
  15. c++ pass return value
  16. return value vs pass by reference
  17. pass function return value javascript
  18. pass function return value
  19. pass return value to another function

I've been trying to build a simple html form that highlights text in yellow on a page by wrapping it in a span on keyup of the form (using jquery). It works fine until I backspace on the keyboard to ...

I've been trying to build a simple html form that highlights text in yellow on a page by wrapping it in a span on keyup of the form (using jquery). It works fine until I backspace on the keyboard to ...

  1. does google index dynamic javascript content

I am trying to implement a graphQL API, it went well with queries but it's going not that well with mutations: Here is my basic mutation using apollo-client and graphql-tag: import gql from '...

I am trying to implement a graphQL API, it went well with queries but it's going not that well with mutations: Here is my basic mutation using apollo-client and graphql-tag: import gql from '...

  1. graphql syntax error expected name found
  2. graphql syntax error expected name found eof
  3. graphql syntax error unexpected eof
  4. graphql syntax error unterminated string
  5. graphql syntax error unexpected name
  6. graphql syntax error expected $
  7. graphql syntax error expected name
  8. graphql syntax error expected found
  9. graphql syntax error expected name found int
  10. graphql syntax error
  11. graphql message syntax error unexpected eof
  12. magento graphql syntax error unexpected eof
  13. syntax error graphql request expected name
  14. syntax error graphql request

I'm trying to find a way to take a link from one browser and open it in another browser. This could be taking a link from a Firefox tab and opening it in Chrome, or taking a link in a Chrome Incognito ...

I'm trying to find a way to take a link from one browser and open it in another browser. This could be taking a link from a Firefox tab and opening it in Chrome, or taking a link in a Chrome Incognito ...