MissingSchemaError MongoDB

时间:2019-09-19 12:25:27

标签: node.js mongodb

我正在使用nodejs,expressjs和mongodb做一个非常基本的SPA应用程序,并且在尝试运行该应用程序时遇到MissingSchemaError。 看来我的数据库模式未正确检测到。 我以为这是一个错字,但是它不是Node js和mongodb的新手,因此能提供一点帮助。

monitoring.model.js

const mongoose = require('mongoose');

var monitoringSchema = new mongoose.Schema({
techName: {
    type: String
},
Customer: {
    type: String
},
Description: {
    type: String
}

});

mongoose.model('monitoring', monitoringSchema);

monitoringController.js

const  express = require('express');
var router = express.Router();
const mongoose = require('mongoose');
const monitoring = mongoose.model('monitoring');


router.get('/', (req, res) => {
res.render("monitoring/addOrEdit", {
    viewTitle : "Insert Problem"
});
});

 router.post('/',(req, res) => {

 console.log(req.body);
 });

 module.exports = router;

2 个答案:

答案 0 :(得分:0)

尝试

Documents

代替

export default mongoose.model('monitoring', monitoringSchema);

您可以了解有关mongoose.model('monitoring', monitoringSchema); 的更多信息 here

答案 1 :(得分:0)

您可以在模式中尝试导出模型。并在控制器中导出模块。

const mongoose = require('mongoose');

var monitoringSchema = new mongoose.Schema({
techName: {
    type: String
},
Customer: {
    type: String
},
Description: {
    type: String
}

});
export default model('Project', ProjectSchema);


const  express = require('express');
var router = express.Router();
const mongoose = require('mongoose');
const monitoringModel = require("../models/monitoring.model");
let monitoringController = {};


router.get('/', (req, res) => {
res.render("monitoring/addOrEdit", {
    viewTitle : "Insert Problem"
});
});

 router.post('/',(req, res) => {

 console.log(req.body);
 });

 module.exports = monitoringController;