Mongoose’s model instance alias User require reported an error when it was introduced.

  mongodb, question

1.

After creating mongoose.model in db.is, assign it to a variable User and then module.exports=User constuser = require (‘../db.js’) .user in User.js; An error was reported while running the program.

2. Code:

//db.js
 
 const mongoose = require('mongoose');
 
 mongoose.connect('mongodb://localhost/vue-login');
 
 let db = mongoose.connection;
 
 //prevent Mongoose: mpromise error
 
 mongoose.Promise = global.Promise;
 
 db.on('error', function(){
 
 Log ('database connection error!'  );
 
 });
 
 db.on('open', function(){
 
 Log ('database connection succeeded!'  );
 
 });
 
 //Declare schema
 
 const userSchema = mongoose.Schema({
 
 username: String,
 
 password: String,
 
 token: String,
 
 create_time: Date
 
 });
 
 //Generate model Based on schema
 
 const User = mongoose.model('User', userSchema)
 
 module.exports = User;
//user.js
 
 const User = require('../db.js').User;
 
 //The following two packets are used to generate time
 
 const moment = require('moment');
 
 const objectIdToTimestamp = require('objectid-to-timestamp');
 
 //for password encryption
 
 const sha1 = require('sha1');
 
 //createToken
 
 const createToken = require('../token/createToken.js');
 
 //database operation
 
 //Find Users by User Name
 
 const findUser = (username) => {
 
 return new Promise((resolve, reject) => {
 
 User.findOne({ username }, (err, doc) => {
 
 if(err){
 
 reject(err);
 
 }
 
 resolve(doc);
 
 });
 
 });
 
 };
 
 //Find all users
 
 const findAllUsers = () => {
 
 return new Promise((resolve, reject) => {
 
 User.find({}, (err, doc) => {
 
 if(err){
 
 reject(err);
 
 }
 
 resolve(doc);
 
 });
 
 });
 
 };
 
 //delete a user
 
 const delUser = function(id){
 
 return new Promise(( resolve, reject) => {
 
 User.findOneAndRemove({ _id: id }, err => {
 
 if(err){
 
 reject(err);
 
 }
 
 Log ('delete user succeeded');
 
 resolve();
 
 });
 
 });
 
 };
 
 //login
 
 const Login = async ( ctx ) => {
 
 //Get Account Number and Password
 
 let username = ctx.request.body.name;
 
 let password = sha1(ctx.request.body.pass);  //Decrypt
 
 let doc = await findUser(username);
 
 if(!  doc){
 
 Log ('User Name Not Found');
 
 ctx.status = 200;
 
 ctx.body = {
 
 info: false
 
 }
 
 }else if(doc.password === password){
 
 Log ('password matches!'  );
 
 //Generate a new token and store it in the database
 
 let token = createToken(username);
 
 console.log(token);
 
 doc.token = token;
 
 await new Promise((resolve, reject) => {
 
 doc.save((err) => {
 
 if(err){
 
 reject(err);
 
 }
 
 resolve();
 
 });
 
 });
 
 ctx.status = 200;
 
 ctx.body = {
 
 success: true,
 
 username,
 
 Token, // Login Successful To create a new token, it should be stored in the database
 
 create_time: doc.create_time
 
 };
 
 }else{
 
 Log ('wrong password!'  );
 
 ctx.status = 200;
 
 ctx.body = {
 
 success: false
 
 };
 
 }
 
 };
 
 //register
 
 const Reg = async ( ctx ) => {
 
 let user = new User({
 
 username: ctx.request.body.name,
 
 Password: sha1 (ctx.request.body.pass),//encryption
 
 Token: create token (this.username),//create token and store in database
 
 Create _ time: moment (objectidtotimestamp (user. _ id)). format ('yyyy-mm-ddhh: mm: ss'),//convert objectid to user creation time
 
 });
 
 //Convert objectid to user creation time (optional)
 
 user.create_time = moment(objectIdToTimestamp(user._id)).format('YYYY-MM-DD HH:mm:ss');
 
 let doc = await findUser(user.username);
 
 if(doc){
 
 Log ('user name already exists');
 
 ctx.status = 200;
 
 ctx.body = {
 
 success: false
 
 };
 
 }else{
 
 await new Promise((resolve, reject) => {
 
 user.save((err) => {
 
 if(err){
 
 reject(err);
 
 }
 
 resolve();
 
 });
 
 });
 
 Log ('registration successful');
 
 ctx.status = 200;
 
 ctx.body = {
 
 success: true
 
 }
 
 }
 
 };
 
 //Get all user information
 
 const GetAllUsers = async( ctx ) => {
 
 //Query all user information
 
 let doc = await findAllUsers();
 
 ctx.status = 200;
 
 ctx.body = {
 
 Successes:' succsess',
 
 result: doc
 
 };
 
 };
 
 //delete a user
 
 const DelUser = async( ctx ) => {
 
 //Get the user id to delete
 
 let id = ctx.request.body.id;
 
 await delUser(id);
 
 ctx.status = 200;
 
 ctx.body = {
 
 Success:' delete succeeded'
 
 };
 
 };
 
 module.exports = {
 
 Login,
 
 Reg,
 
 GetAllUsers,
 
 DelUser
 
 };

3. Login error report

TypeError: Cannot read property 'findOne' of undefined
 at Promise (E:\hongshi\report1\test-login\src\server\controller\user.js:192:10)
 at new Promise (<anonymous>)
 at findUser (E:\hongshi\report1\test-login\src\server\controller\user.js:190:10)
 at Login (E:\hongshi\report1\test-login\src\server\controller\user.js:268:19)
 at dispatch (E:\hongshi\report1\test-login\node_modules\koa-compose\index.js:44:32)
 at next (E:\hongshi\report1\test-login\node_modules\koa-compose\index.js:45:18)
 at E:\hongshi\report1\test-login\node_modules\koa-router\lib\router.js:346:16
 at dispatch (E:\hongshi\report1\test-login\node_modules\koa-compose\index.js:44:32)
 at E:\hongshi\report1\test-login\node_modules\koa-compose\index.js:36:12
 at dispatch (E:\hongshi\report1\test-login\node_modules\koa-router\lib\router.js:351:31)
 at dispatch (E:\hongshi\report1\test-login\node_modules\koa\node_modules\koa-compose\index.js:42:32)
 at bodyParser (E:\hongshi\report1\test-login\node_modules\koa-bodyparser\index.js:86:11)
 at process._tickCallback (internal/process/next_tick.js:68:7)

Error in registration

ReferenceError: user is not defined
 at Reg (E:\hongshi\report1\test-login\src\server\controller\user.js:352:45)
 at dispatch (E:\hongshi\report1\test-login\node_modules\koa-compose\index.js:44:32)
 at next (E:\hongshi\report1\test-login\node_modules\koa-compose\index.js:45:18)
 at E:\hongshi\report1\test-login\node_modules\koa-router\lib\router.js:346:16
 at dispatch (E:\hongshi\report1\test-login\node_modules\koa-compose\index.js:44:32)
 at next (E:\hongshi\report1\test-login\node_modules\koa-compose\index.js:45:18)
 at allowedMethods (E:\hongshi\report1\test-login\node_modules\koa-router\lib\router.js:407:12)
 at dispatch (E:\hongshi\report1\test-login\node_modules\koa-compose\index.js:44:32)
 at next (E:\hongshi\report1\test-login\node_modules\koa-compose\index.js:45:18)
 at E:\hongshi\report1\test-login\node_modules\koa-router\lib\router.js:346:16
 at dispatch (E:\hongshi\report1\test-login\node_modules\koa-compose\index.js:44:32)
 at E:\hongshi\report1\test-login\node_modules\koa-compose\index.js:36:12
 at dispatch (E:\hongshi\report1\test-login\node_modules\koa-router\lib\router.js:351:31)
 at dispatch (E:\hongshi\report1\test-login\node_modules\koa\node_modules\koa-compose\index.js:42:32)
 at bodyParser (E:\hongshi\report1\test-login\node_modules\koa-bodyparser\index.js:86:11)
 at process._tickCallback (internal/process/next_tick.js:68:7)

new mongoose.Schema({})
NeednewAn example