module.js:549抛出错误;错误:找不到模块'./models/TodoListModel'Nodemon在添加模型文件时显示此错误

时间:2018-07-23 10:38:15

标签: node.js mongodb express nodemon

当我尝试在我的server.js文件中添加模型文件ToDoListModel.js时,显示以下错误:-

**module.js:549
    throw err;
    ^**

    **Error: Cannot find module './models/TodoListModel'**
        at Function.Module._resolveFilename (module.js:547:15)
        at Function.Module._load (module.js:474:25)
        at Module.require (module.js:596:17)
        at require (internal/module.js:11:18)
        at Object.<anonymous> (D:\xampp\htdocs\todolist\server.js:28:1)
        at Module._compile (module.js:652:30)
        at Object.Module._extensions..js (module.js:663:10)
        at Module.load (module.js:565:32)
        at tryModuleLoad (module.js:505:12)
        at Function.Module._load (module.js:497:3)
        at Function.Module.runMain (module.js:693:10)
        at startup (bootstrap_node.js:191:16)
        at bootstrap_node.js:612:3
    [nodemon] app crashed - waiting for file changes before starting

...


Project Folder Structure

在server.js中添加foenter图像描述herellowing代码时,它显示错误-

module.exports = mongoose.model('Tasks', TaskSchema);
var Task = mongoose.model('Tasks');

架构是在ToDoListModel.js中创建的

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

var TaskSchema = new Schema({
    name: {
       type: String,
       require: 'Kindly enter the name of the task'
     },
    Created_date: {
       type: Date,
       default: Date.now
     },
   status: {
      type: [{
          type: String,
          enum: ['pending', 'ongoing', 'completed']
       }],
    default: ['pending']
    }
 });
module.exports = mongoose.model('Tasks', TaskSchema);

将寻求任何帮助。

2 个答案:

答案 0 :(得分:1)

server.js文件无法找到您的TodoListModel.js文件。 您应该在 server.js 中提供模型文件的确切路径, 试试这个会起作用。

    var Tasks = require('./app/models/TodoListModel');

答案 1 :(得分:0)

如果我正确理解了问题,则只需执行以下操作即可访问Tasks对象:

var Tasks = require('./ models / TodoListModel');