I am working on a project and the current implementation that it has defines the array currentTerm when the server is started. Since the admin of the site can go into settings and change which are the current terms, I would like to be able to change this array every time a change is made.
However, I am making the same call to change the array but the contents of it are not being changed.
Here is the code.
I have tried changing the function so that it returns an array with the new current semesters, so that set the array to be what the function returns, but the function seems to be returning unidentified
This is what I have right now, I push the terms into currentTerms, but whenever I call the function again, the contents of the array are still the same. (Basically it only keeps the terms that it got from the first call)
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);
});
});
}
This is my modified function. Here I try to make a temporal array that will be returned and then setup the currentTerms to be equal to the temporal array. However, the return type seems to be undefined every time. I have tried putting it in all the positions that are commented out (1,2,3) and they all return undefined.
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;
}
As mentioned before, I would like to be able to change the array, so that whenever the admin changes the current terms in the db, these changes are reflected in the array.
For example: 1. When the server starts the current terms are: Spring 2019, Summer 2019, and Fall 2019.
- Admin gets into the website settings and changes the current semesters to only be Spring 2019.
The array should now include only 'Spring 2019', but the array keeps the three semesters that it got when the server was started.
Any sort of help would be highly appreciated!