Sunday, 16 April 2017

RegExp exec issue

The code below verifies that the requested REST endpoint is in the form of '/employees/<[A-Za-z0-9_]+>" The following endpoint would be valid "/employees/100". IF the endpoint is valid it then excludes the requested url and just passes the employee number to the emp.get procedure. The codes is not performing the correct ReqExp.exec command and is passing /employees/100 when it should just passing 100. I have included the log messages for empno and req.url.

I am not sure if the problem is the sql query that is getting executed or passing of the employee number to the sql query. The sql query should be performing a where statement on employee_number column.

Any help would be greatly appreciated.

        var empnoPatt = "[A-Za-z0-9_]+";
        var patt = new RegExp("/employees/" + empnoPatt);
        if (patt.test(req.url)) {
            patt = new RegExp(empnoPatt);
            console.log(req.url);
            var empno = patt.exec(req.url);
            console.log(empno);
            emp.get(req, resp, empno);
        }

console.log(req.url);
/employees/100

console.log(empno);
[ 'employees', index: 1, input: '/employees/100' ]

this is the error that I am receiving when I try to search for employee 100
{ RequestError: Invalid column name 'employees'.  This is confusion to me and I am not sure why the sql query thinks employees is a column in the table.

This the function that gets executed that should return employee record where employee_number = 100

exports.get = function (req, resp, empno) {
    db.executeSql("SELECT * FROM EMPLOYEE WHERE EMPLOYEE_NUMBER =" + empno, function (data, err) {
        if (err) {
            httpMsgs.show500(req, resp, err);
        }
        else {
            httpMsgs.showJSON(req, resp, data);
        }
    });



via joey.coyle

No comments:

Post a Comment