Node.js猫鼬中找不到模块错误

时间:2018-12-17 09:08:19

标签: node.js mongodb express mongoose

我正在尝试运行此代码来尝试为我的猫鼬数据库创建新用户。但是,每当尝试启动服务器时,都会出现错误消息

  

错误:找不到模块'../ models / registeruser'

在我的终端上。我已尝试解决此问题,但似乎无法解决。我的代码如下,以及目录结构的屏幕快照,其中每个文件夹中的文件均可见。谁能帮我解决这个问题?谢谢!

index.ejs

<!DOCTYPE html>
    <html>
      <head>
        <title>Register</title>
      </head>
      <body>
        <h1>Enter the following information</h1>
        <form method="post" action="/executive/create">
        <label>Enter Your Name</label><br>
        <input type="text" name="firstName" placeholder="Enter first name" required>
        <input type="text" name="lastName" placeholder="Enter last name" required>
        <input type="text" name="userName" placeholder="Enter a username" required>
        <input type="text" name="email" placeholder="Enter your email" required>
        <input type="submit" value="Register User">
        </form>
        </body>
    </html>

executive.js

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

var siteController = require('../controllers/siteController');

router.get('/create', siteController.registerusercreate);

module.exports = router;

registeruser.js

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

var UserSchema = Schema({
    firstName: {type: String, required: true},
    lastName: {type: String, required: true},
    userName: {type: String, required: true},
    email: {type: String, required: true}
});

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

siteController.js

var User = require('../models/registeruser');

exports.registerusercreate = function (req, res) {
    let user = new User(
        {
            firstName: req.body.firstName,
            lastName: req.body.lastName,
            userName: req.body.userName,
            email: req.body.email
        }
    );

    user.save(function (err) {
        if (err) {
            return next(err);
        }
        console.log("Item Saved Successfully!");
    })
};

app.js

var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');

var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var executiveRouter = require('./routes/executive'); 

var app = express();

var mongoose = require('mongoose');
var dev_db_url = 'mongodb://username:<password@ds042908.mlab.com:42908/users'
var mongoDB = process.env.MONGODB_URI || dev_db_url;
mongoose.connect(mongoDB);
mongoose.Promise = global.Promise;
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'MongoDB connection error:'));

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', indexRouter);
app.use('/users', usersRouter);
app.use('/executive', executiveRouter);  // Add catalog routes to middleware chain.

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;

Here is a screenshot of my file directory with all files

2 个答案:

答案 0 :(得分:0)

在Executive.js中,您已将siteController导入为usernameController,但是您正在使用siteController.registerusercreate来调用该函数。尝试解决此问题,是否可以在所有文件夹都打开的情况下发布目录结构的屏幕快照?

答案 1 :(得分:0)

是的,模型文件中存在拼写错误。您已将其命名为“ regsiteruser.js”,请更正。