NodeJS API:无法将2个参数传递给请求响应

时间:2018-06-18 18:48:13

标签: node.js api httprequest httpresponse

我无法通过请求响应功能传递2个ID。我可以传递1个id /参数而没有任何问题但是在下面传递2个参数时遇到问题是我的代码片段:

服务器:

var http = require("http");
var org = require("../controllers/org");
var school = require("../controllers/school");
var academicSession = require("../controllers/academicSession");
var term = require("../controllers/term");
var gradingperiod = require("../controllers/gradingperiod");
var course = require("../controllers/course");
var stu = require("../controllers/student");
var cls = require("../controllers/class");
var user = require("../controllers/user");
var demo = require("../controllers/demographic");
var enroll = require("../controllers/enrollment");
var teach = require("../controllers/teacher");
var crscls = require("../controllers/schoolcs");
var enr = require("../controllers/enrollqrys");
var settings = require("../settings");
var httpMsgs = require("../core/httpMsgs");


http.createServer(function (req, resp) {
    switch (req.method) {
        case "GET":

            if (req.url === "/") {
                httpMsgs.showHome(req, resp);
            }
            else if (req.url === "/orgs") {
                org.getOrgs(req, resp);
            }
            else if (req.url.match("/orgs/[0-9]+$")) {
                var idorg = "[0-9]+";
                var patt = new RegExp("/orgs/" + idorg);
                if (patt.test(req.url)) {
                    patt = new RegExp(idorg);
                    var id = patt.exec(req.url);
                    org.getOrg(req, resp, id);
                }
                else {
                    httpMsgs.show404(req, resp);
                }
            }

            ***else if (req.url.match("/schools/[0-9]+[A-Za-z0-9\-\_]+/classes/[0-9]+[A-Za-z0-9\-\_]+/enrollments$")) {
                var idcl = "[0-9]+[A-Za-z0-9\-\_]+";
                var idsc = "[0-9]+[A-Za-z0-9\-\_]+";
                var str = idcl + "[0-9]+[A-Za-z0-9\-\_]+" + idsc;
                var patt = new RegExp("/schools/" + idcl + "/classes/" + idsc + "/enrollments");
                if (patt.test(req.url)) {
                    patt = new RegExp(str);
                    var id = patt.exec(req.url);
                    enr.getEnrollmentsForClassInSchools(req, resp, id, arg2);
                }
                else {
                    httpMsgs.show404(req, resp);
                }
            }***

enrollqrys.js:

var db = require("../core/db");
var httpMsgs = require("../core/httpMsgs");


exports.getEnrollmentsForClassInSchools = function (req, resp, id, arg2) {
    db.executeSql("EXEC dbo.getEnrollmentsForClassInSchools @clid = '" + id + "'" + ", @scid = '" + arg2 + "'", function (data, err) {
        if (err) {

            httpMsgs.show500(req, resp, err);
        }
        else {
            //resp.writeHead(200, { "Content-Type": "application/json" });
            //resp.write(JSON.stringify(data));
            //resp.end();
            httpMsgs.sendJson(req, resp, data);
        }


    });

};

exports.getStudentsForClassInSchool = function (req, resp, id, id) {
    db.executeSql("EXEC dbo.getEnrollmentsForClassInSchools @clid = '" + clid + "'" + ", @scid =" + scid, function (data, err) {
        if (err) {

            httpMsgs.show500(req, resp, err);
        }
        else {
            //resp.writeHead(200, { "Content-Type": "application/json" });
            //resp.write(JSON.stringify(data));
            //resp.end();
            httpMsgs.sendJson(req, resp, data);
        }


    });

};

测试此GET时出现未引用的错误。任何帮助将不胜感激。我将补充说我是javascript和nodejs的新手

以下错误:

开始收听:9000 C:\ Users \用户THOMMA02 \源\回购\ NodejsConsoleApp1 \ NodejsConsoleApp1 \核心\ server.js:281                     enr.getEnrollmentsForClassInSchools(req,resp,id,arg2);                                                                        ^

ReferenceError:未定义arg2     在服务器上。 (C:\ Users \用户THOMMA02 \源\回购\ NodejsConsoleApp1 \ NodejsConsoleApp1 \核心\ server.js:281:72)     在emitTwo(events.js:126:13)     在Server.emit(events.js:214:7)     在parserOnIncoming(_http_server.js:619:12)     在HTTPParser.parserOnHeadersComplete(_http_common.js:112:17) 等待调试器断开连接......

0 个答案:

没有答案