module.js:549 throw err; Error: Cannot find module './models/TodoListModel' Nodemon shows this error on adding model file

module.js:549 throw err; Error: Cannot find module './models/TodoListModel' Nodemon shows this error on adding model file

当我尝试在我的 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

...


当在 server.js 中添加 foenter image description herelowing code 时显示错误-

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);

任何帮助都将适用。

如果我对问题的理解正确,您应该能够通过执行以下操作访问 Tasks 对象:

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

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

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