c:\ Users \ fc \ Desktop \ projector \ node_modules \ mongoose \ lib \ index.js:497 抛出新的mongoose.Error.MissingSchemaError(name); ^ MongooseError [MissingSchemaError]:尚未为模型“ Employee”注册架构。 使用mongoose.model(名称,模式) 在新的MissingSchemaError(c:\ Users \ fc \ Desktop \ projector \ node_modules \ mongoose \ lib \ error \ missingSchema.js:22:11) 在Mongoose.model(c:\ Users \ fc \ Desktop \ projector \ node_modules \ mongoose \ lib \ index.js:497:13) 在对象。
Blockquote
const express=require('express');
var router=express.Router();
const mongoose=require('mongoose');
//var Employee=require('../models/employee.model');
var Employee = mongoose.model('Employee');
router.get('/', (req,res)=>{
res.render('C:/Users/fc/Desktop/projector/views/employee/addOrEdit.hbs', {
viewTitle: "Insert Radio Employee Data"
})
});
router.post('/', (req,res)=>{
//before insertRecord Function below console.log(req.body); executed
// console.log('New Employee Data has been Received!');
// console.log(req.body);
insertRecord(req, res);
});
// insertRecord function is use to insert data in mongoDB
function insertRecord(req, res){
var employee= new Employee();
employee.fullName=req.body.fullName;
employee.email=req.body.email;
employee.mobile=req.body.mobile;
employee.city=req.body.city;
employee.save((err,doc)=>{
if(!err){
res.redirect('employee/list');
}
else{
console.log('An Error has been found:', err);
}
})
}
router.get('/list', (req,res)=>{
res.json('from list');
});
// here one function is going to be exported
module.exports=router;
答案 0 :(得分:0)
错误提示,您缺少猫鼬模式。您正在尝试创建模型,但是没有任何模式。这是一个如何创建猫鼬模式的示例:
const express = require('express');
var router = express.Router();
const mongoose = require('mongoose');
//var Employee=require('../models/employee.model');
/**
* Here is the mongoose schema .
*/
const employeeSchema = new mongoose.Schema({
fullName: { type: String, default: '' },
email: { type: String, default: '' },
mobile: { type: String, default: '' },
city: { type: String, default: '' }
});
/**
* Create model using mongoose schema.
*/
var Employee = mongoose.model('Employee', employeeSchema);
router.get('/', (req, res) => {
res.render('C:/Users/fc/Desktop/projector/views/employee/addOrEdit.hbs', {
viewTitle: "Insert Radio Employee Data"
})
});
router.post('/', (req, res) => {
//before insertRecord Function below console.log(req.body); executed
// console.log('New Employee Data has been Received!');
// console.log(req.body);
insertRecord(req, res);
});
// insertRecord function is use to insert data in mongoDB
function insertRecord(req, res) {
var employee = new Employee();
employee.fullName = req.body.fullName;
employee.email = req.body.email;
employee.mobile = req.body.mobile;
employee.city = req.body.city;
employee.save((err, doc) => {
if (!err) {
res.redirect('employee/list');
}
else {
console.log('An Error has been found:', err);
}
})
}
router.get('/list', (req, res) => {
res.json('from list');
});
// here one function is going to be exported
module.exports = router;