先生们美好的一天,这里的初学者 react ,表达 mvc 架构......
我知道这是一个 react.js 社区,但我会要求服务器端..
我基于应用程序创建 express-mysql-react 应用程序的教程
本教程的一部分讨论了 mvc 架构的创建,它创建了一个表示数据库表的模型,用于查询连接的 Controller ,然后将结果用作 api..我已经完成了本教程的基本目标,我想展开它。
我的目标是将legend.controller.js 的findAll() 函数的结果获取到我的enrollmentcount.controller.js 的findAll() 函数中,因为我想用legend Controller 的查询结果来查询注册计数 Controller 的 findAll()。
到目前为止,这些是收集到的代码
这是enrollmentcount.controller.js 的代码
const db= require('../models');
const legend = require('./legend.controller.js')
const enrollmentcount = db.enrollmentcount;
const Op = db.Sequelize.Op;
// Retrieve all enrollmentcount from the database.
exports.findAll = (req,res) =>{
const coursecode = req.query.title;
//get the query result values from the legend controller...
const currsemyear = legend.getCurrentYearSem().then(function(result){console.log(result.semester);});
console.log(currsemyear);
var condition = coursecode ?{ title: {[Op.like]:`%${coursecode}%`}} : null;
enrollmentcount.findAll({ where: condition})
.then(data=>{
res.send(data);
})
.catch(err =>{
res.status(500).send({
message:err.message || "Some error occured while retrieving enrollmentcount. "
});
});
};
// Find a single enrollmentcount with an id
exports.findOne= (req,res) =>{
const id = req.params.id;
enrollmentcount.findByPk(id)
.then(data => {res.send(data);
})
.catch(
err=>{
res.status(500).send({
message: "Error retrieving enrollmentcount with id=" + id
});
});
};
legend.controller.js
const db= require('../models');
const legend = db.legend;
const Op = db.Sequelize.Op;
function getCurrentYearSem(){
return legend.findAll({
attributes: ['semester', 'schoolyear']
});
}
exports.findAll = (req,res) =>{
legend.findAll(
{
attributes: ['semester', 'schoolyear']
})
.then(data=>{
res.send(data);
})
.catch(err =>{
res.send({ message:err.message || "Some error occured while retrieving current semester and current schoolyear. "});
});
};
注册计数.controller.js
module.exports = app =>{
const enrolledcount = require("../controllers/enrollmentcount.controller.js");
var router = require("express").Router();
router.get("/",enrolledcount.findAll);
router.get("/:coursecode",enrolledcount.findOne);
app.use("/api/enrollmentcount",router);
};
图例.routes.js
module.exports = app =>{
const legend = require("../controllers/legend.controller.js");
var router = require("express").Router();
router.get("/",legend.findAll);
app.use("/api/legend",router);
};
我对这项技术完全陌生,很抱歉,提前谢谢你
最佳答案
一些建议,希望对您有所帮助。
这是一个简短的示例,可以向您展示我的意思(未经测试,因此您必须对其进行一些清理):
注册计数.controller.js
// Retrieve all enrollmentcount from the database.
exports.findAll = async (req,res) => {
const coursecode = req.query.title;
const enrollmentCount = await schoolService.getEnrollmentCount(courseCode);
res.send(data);
}
school.service.js
const legendRepo = require('../repositories/legend.repository');
const enrollmentRepo = require('../repositories/enrollment.repository');
exports.getEnrollmentCount = async (courseCode) {
const currentYearSem = await legendRepo.getCurrentYearSem();
console.log(currentYearSem);
const condition = coursecode ? { title: {[Op.like]:`%${coursecode}%`}} : null;
return enrollmentRepo.getEnrollmentCount(condition);
}
legend.repository.js
const db= require('../models');
const legend = db.legend;
exports.getCurrentYearSem = () => {
return legend.findAll({
attributes: ['semester', 'schoolyear']
});
}
注册.repository.js
const db = require('../models');
const enrollmentCount = db.enrollmentcount;
exports.getEnrollmentCount = (condition) => {
return enrollmentCount.findAll({ where: condition} );
}
关于javascript - 在 express 中访问 Controller 的查询结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61182193/