如何修复Node.js中的架构错误?

  1. the error C:\Users\Jurjiu\Desktop\shit\node_modules\mongoose\lib\index.js:502 throw new mongoose.Error.MissingSchemaError(name); ^ MongooseError [MissingSchemaError]: Schema hasn't been registered for model "Employee". Use mongoose.model(name, schema) at new MissingSchemaError (C:\Users\Jurjiu\Desktop\shit\node_modules\mongoose\lib\error\missingSchema.js:22:11) at Mongoose.model (C:\Users\Jurjiu\Desktop\shit\node_modules\mongoose\lib\index.js:502:13) at Object.<anonymous> (C:\Users\Jurjiu\Desktop\shit\controllers\employeeController.js:4:25) at Module._compile (internal/modules/cjs/loader.js:1133:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1153:10) at Module.load (internal/modules/cjs/loader.js:977:32) at Function.Module._load (internal/modules/cjs/loader.js:877:14) at Module.require (internal/modules/cjs/loader.js:1019:19) at require (internal/modules/cjs/helpers.js:77:18) at Object.<anonymous> (C:\Users\Jurjiu\Desktop\shit\server.js:8:26) at Module._compile (internal/modules/cjs/loader.js:1133:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1153:10) at Module.load (internal/modules/cjs/loader.js:977:32) at Function.Module._load (internal/modules/cjs/loader.js:877:14) at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12) at internal/main/run_main_module.js:18:47 [nodemon] app crashed - waiting for file changes before starting...
  2. Code- from employeeController.js

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

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

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

    function InsertRecord(req,res){ var employee = new Employee(); employee.fullName = req.body.fullName; employee.mail = 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('Error during record insertion : ' + err);

     }
    

    });}

    router.get('/list', ( req , res) => { res.json('from list'); });

    module.exports = router;

  3. Employee

    `const mongoose=require('mongoose');

    var employeeSchema=new mongoose.Schema({ fullname:{ type : String }, email:{ type : String }, mobile: { type : String }, mobile: { type : String }

    });

    mongoose.model('Employee',employeeSchema); module.exports={Employee};`

评论
hehe
hehe

您需要分配模型,通过

require('../models/[your_model]');

var User = mongoose.model('./models/['model']');```
点赞
评论