我收到错误“错误:找不到模块'./todo.model'”
模式在同一文件夹中 这是文件夹内容: -后端 -数据:db -node_modules -todo.model.js -package.json -server.js
这是 server.js
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const cors = require('cors');
const mongoose = require('mongoose');
const todoRoutes = express.Router();
const PORT = 4000;
**var Todo = require('./todo.model');**
app.use(cors());
app.use(bodyParser.json());
mongoose.connect('mongodb://127.0.0.1:27017/todos', { useNewUrlParser: true });
const connection = mongoose.connection;
connection.once('open', function() {
console.log("MongoDB database connection established successfully");
})
这是架构
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
let Todo = new Schema({
todo_description: {
type: String
},
todo_responsible: {
type: String
},
todo_priority: {
type: String
},
todo_completed: {
type: Boolean
}
});
module.exports = mongoose.model('Todo', Todo);
答案 0 :(得分:0)
我假设 require
对文件扩展名感到困惑,认为文件实际上是.model
扩展名。
尝试更加明确,包括.js
require('./todo.model.js');