我正在研究一个项目,它的当前实现是在启动服务器时定义了currentTerm数组。由于该站点的管理员可以进入设置并更改当前条款,因此我希望每次更改后都可以更改此数组。
但是,我正在进行相同的调用以更改数组,但其内容未更改。
这是代码。
我尝试过更改该函数,以便它以新的当前学期返回一个数组,以便将该数组设置为该函数返回的内容,但该函数似乎返回的是未知的
这就是我现在所拥有的,我将术语推入currentTerms中,但是每当我再次调用该函数时,数组的内容仍然相同。 (基本上,它只保留第一次调用时获得的条款)
var bodyParser = require('body-parser');
var Project = require('../models/projects');
var Term = require('../models/terms');
var authProvider = require('../services/AuthorizationProvider');
module.exports = function (app, express) {
var apiRouter = express.Router();
var currentTerm = [];
var findActiveTerm = function () {
Term.find({
'status.currentSemester': true
}, '_id',
function (err, terms) {
if (err || !terms) { // Failed to find the current semester
terms = Term.createDefaultTerm();
}
terms.map(function (term) {
currentTerm.push(term._id);
})
});
}
//Find the active term and set currentTerm to it
findActiveTerm();
//route get or adding projects to a users account
apiRouter.route('/projects')
.post(
authProvider.authorizeByUserType([authProvider.userType.PiCoPi, authProvider.userType.StaffFaculty]),
function (req, res) {
console.log(req.body.faculty);
//req.body.term = currentTerm; // TODO: Should not automatically set new projects to be set to currentTerm
//Validate to ensure student counts isn't negative or student count is greater than maximum.
var studentCount = 0;
var maxStudentCount = 0;
// user provided a min number of students
if (req.body.firstSemester)
studentCount = Number(req.body.firstSemester);
// user provided a max number of students
if (req.body.maxStudents)
maxStudentCount = Number(req.body.maxStudents);
// user didnt supply a min and max number of students, make it a really big number so anyone can join
if (isNaN(studentCount) || isNaN(maxStudentCount)) {
req.body.firstSemester = "1";
req.body.maxStudents = "256";
}
// user didnt supply a min and max number of students, make it a really big number so anyone can join
if (studentCount == 0 && maxStudentCount == 0) {
req.body.firstSemester = "1";
req.body.maxStudents = "256";
}
if (studentCount < 0 || maxStudentCount < 0) {
res.status(400);
return res.send("firstSemester cannot be less than 0 or maxStudents cannot be less than 0.");
}
if (studentCount > maxStudentCount) {
res.status(400);
return res.send("Count cannot be greater than the maximum.");
}
Project.create(req.body, function (err) {
if (err) {
res.status(400);
return res.send(err);
}
return res.json({
success: true
});
});
})
.get(
authProvider.authorizeAll,
function (req, res) {
//findActiveTerm();
console.log(currentTerm);
Project.find({
//term: currentTerm
term: currentTerm
}, function (err, projects) {
if (err) {
console.log(err);
return res.send('error');
}
return res.json(projects);
});
});
}
这是我修改的功能。在这里,我尝试制作一个将返回的时间数组,然后将currentTerms设置为与该时间数组相等。但是,返回类型似乎每次都未定义。我尝试将其放在所有已注释掉的位置(1,2,3)中,并且它们都返回未定义状态。
var findActiveTerm = function () {
var tempArr = [];
Term.find({
'status.currentSemester': true
}, '_id',
function (err, terms) {
if (err || !terms) { // Failed to find the current semester
terms = Term.createDefaultTerm();
}
terms.map(function (term) {
//currentTerm.push(term._id);
tempArr.push(term._id);
//1
return tempArr;
})
//2
return tempArr;
});
//3
return tempArr;
}
如前所述,我希望能够更改数组,以便每当管理员更改数据库中的当前术语时,这些更改就会反映在数组中。
例如: 1.服务器启动时,当前条款为:2019年春季,2019年夏季和2019年秋季。
该数组现在应该仅包含“ Spring 2019”,但是该数组保留了启动服务器时获得的三个学期。
任何帮助将不胜感激!