gpt4 book ai didi

node.js - 如何在 Mongoose 的同一个文件中导出多个模式

转载 作者:行者123 更新时间:2023-12-02 16:52:10 25 4
gpt4 key购买 nike

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

这是我的 certification.js 文件(模型)

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 文件(路由)

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 未定义

最佳答案

将此代码更改为 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()函数

关于node.js - 如何在 Mongoose 的同一个文件中导出多个模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58172762/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com