首页 > 解决方案 > 函数返回未定义且数组未更新

问题描述

我正在开发一个项目,它在服务器启动时定义了数组 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 年秋季。

  1. 管理员进入网站设置并将当前学期更改为仅 2019 年春季。

该数组现在应该只包含“2019 年春季”,但该数组保留了服务器启动时获得的三个学期。

任何形式的帮助将不胜感激!

标签: javascriptmongodbexpress

解决方案


推荐阅读