【问题标题】:module.js:549 throw err; Error: Cannot find module './models/TodoListModel' Nodemon shows this error on adding model filemodule.js:549 抛出错误;错误:找不到模块 './models/TodoListModel' Nodemon 在添加模型文件时显示此错误
【发布时间】:2018-12-30 18:57:31
【问题描述】:

当我尝试在我的 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 图像描述如下代码时显示错误-

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

我们将提供任何帮助。

【问题讨论】:

    标签: node.js mongodb express nodemon


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      如果我正确理解了这个问题,您应该可以通过这样做来访问 Tasks 对象:

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

      【讨论】:

      • 是的。我想在 server.js 文件中使用 ToDoListModel.js 文件。但找不到确切的解决方案
      猜你喜欢
      • 2018-02-26
      • 1970-01-01
      • 2017-01-23
      • 2013-09-29
      • 2018-08-06
      • 1970-01-01
      • 2018-02-12
      • 2018-12-03
      • 2017-03-02
      相关资源
      最近更新 更多