如何在Mongoose中导出同一文件中的多个Schemas

ybzsozfc  于 2023-10-19  发布在  Go
关注(0)|答案(2)|浏览(108)

我想在我的模型js文件中导出两个模式,我想在路由器js文件中使用。我试过下面的代码。

这是我的certification.js文件(models)

const mongoose = require('mongoose');
const Schema = mongoose.schema;

const requestCertificationSchema = mongoose.Schema({
 userid: { type: String, require: true },
 certName: { type: String, require: true },
 certType: { type: String, require: true },
 examName: { type: String, require: true },
 examYear: { type: String, require: true },
 examIndex: { type: String,  require: true },
 reqDate: {type: String, require: true},
 state: { type: String, require: true}
});

const requestCertification = mongoose.model("requestCertification", requestCertificationSchema);

module.exports.saveRequest = function (newRequestCertification, callback) {
     newRequestCertification.save(callback);
};

const requestStudentstatusSchema = mongoose.Schema({
 studentName: { type: String, require: true },
 admissionNum: { type: String, require: true },
 dateofAdmission: { type: String, require: true },
 currentStatus: { type: String, require: true },
 description: { type: String, require: true },
});

const requestStudentstatus=  mongoose.model("requestStudentstatus", requestStudentstatusSchema);

module.exports = {
  requestCertification: requestCertification,
  requestStudentstatus: requestStudentstatus
}

这是我的certification.js文件(routes)

const router = express.Router();
const Certification = require('../models/certification');
const config = require('../config/database');

router.post("/requestCert", function (req, res) {
    const newRequest = new requestCertification({
        userid: req.body.userid,
        certName: req.body.certName,
        certType: req.body.certType,
        examName: req.body.examName,
        examYear: req.body.examYear,
        examIndex: req.body.examIndex,
        reqDate:req.body.reqDate,
        state: req.body.state
    });
    console.log(newRequest);
    Certification.requestCertification.saveRequest(newRequest, function (err, request) {
        if (err) {
            res.json({ state: false, msg: "Data inserting Unsuccessfull..!" });
        }
        if (request) {
            res.json({ state: true, msg: "Data inserted Successfully..!" });
        }
    });
});

当我调用这个URL时,我显示了这个错误!.

ReferenceError:requestCertification未定义

ig9co6j1

ig9co6j11#

将此代码更改为routes/certification.js中的此代码

const router = express.Router();
const {requestCertification} = require('../models/certification');
const {requestStudentstatus} = require('../models/certification');
const config = require('../config/database');

router.post("/requestCert", function (req, res) {
    const newRequest = new requestCertification({
        userid: req.body.userid,
        certName: req.body.certName,
        certType: req.body.certType,
        examName: req.body.examName,
        examYear: req.body.examYear,
        examIndex: req.body.examIndex,
        reqDate:req.body.reqDate,
        state: req.body.state
    });
    // console.log(newRequest);
    newRequest
        .save()
        .then(result => {
            console.log(result)
            res.json({ state: true, msg: "Data inserted Successfully..!" });
        })
        .catch(error => {
            console.log(error)
            res.json({ state: false, msg: "Data inserting Unsuccessfull..!" });
        })
});

并删除models/certification.js中的**saveRequest()**函数

wgeznvg7

wgeznvg72#

const router = express.Router();
const { requestCertification, requestStudentstatus } = require('../models/certification');
const config = require('../config/database');

router.post("/requestCert", function (req, res) {
    const newRequest = new requestCertification({
        userid: req.body.userid,
        certName: req.body.certName,
        certType: req.body.certType,
        examName: req.body.examName,
        examYear: req.body.examYear,
        examIndex: req.body.examIndex,
        reqDate: req.body.reqDate,
        state: req.body.state
    });
    // console.log(newRequest);
    newRequest
        .save()
        .then(result => {
            console.log(result)
            res.json({ state: true, msg: "Data inserted Successfully..!" });
        })
        .catch(error => {
            console.log(error)
            res.json({ state: false, msg: "Data inserting Unsuccessfull..!" });
        })
});

相关问题