Search code examples
sql-serverangularexpressnode-mssql

Angular client receives OK as error for POST req from server with Expressjs


I am trying to execute a stored procedure developed in SQL Server through node-mssql package. For server side, I have used ExpressJS, client side is developed using Angular.

My problem is: node-mssql executes the stored procedure without any issues, and returns no. of rows affected. But on the client side, in the console, I get the Error as OK, however the response status in 200. Because of this error, page doesn't redirect to the next page. Would any one explain me it assist me to fix this issue? I don't know where I have missed.

I tried to for inserting the data to DB as normal query (without stored procedure), using req.query method. It works fine both on server and client side. I believe the error message is responded only for req.execute executing a stored procedure. I prefer to use the stored procedure rather than normal SQL command for inserting data into the table.

Client-side Http POST Req:

createFlexiClaim(userid: any, claimClass: any, flexiClaim): Observable<void> {
    return this.http.post<void>(DBServerUtil.Uri + DBServerUtil.port + '/create/' + claimClass + '/' + userid, flexiClaim, 
    {
      headers: new HttpHeaders({
        'content-Type': 'application/json; charset=utf-8'
      })
    }
    )
    .pipe(catchError(this.handleError));
  }
}

ExpressJS POST request:

app.post('/create/:claimClass/:userid', function (req, res) {
    // (sqlqry,ClaimWBS,Docdate,typeclaim,curr,AMT,typec,empid,empname,claimfor,forrela, callback)
    // console.log(req.body.projectWbs + " : " + req.body.docDate + " : " + req.body.typeOfClaim + " : " + req.body.currencyKey + " : " + req.body.amount + " : " + req.params.claimClass + " : " + req.params.userid + " : " + req.params.userid + " : " + ":" + req.params.claimClass + " : " + req.params.userid)
    queryExe.CreateclaimPromise('CREATECLAIMDTL',req.body.projectWbs, req.body.docDate, req.body.typeOfClaim, req.body.currencyKey, req.body.amount, req.params.claimClass, req.params.userid, req.params.userid, '', '', function (recordRet, err) {
        if (err) {
            res.send("Error from app.js : "+err)
        } else {
            console.log("Message from app.js : "+recordRet)
            res.send("Message from app.js : "+JSON.stringify(recordRet))
        }
    })
})

node-mssql connection request:

sql.connect(setting.dbconfig).then(pool => {
        return pool.request()
            .input('PROJWBS', sql.VarChar(10), ClaimWBS)
            .input('DOCUMENTDATE', sql.Date, Docdate)
            .input('TYPEOFSUBCLAIM', sql.Int, typeclaim)
            .input('CURRENCY', sql.VarChar(5), curr)
            .input('AMOUNT', sql.Money, AMT)
            .input('CLAIMTYPE', sql.VarChar(10), typec)
            .input('EMPLOYEEID', sql.VarChar(50), empid)
            .input('EMPLOYEENAME', sql.VarChar(150), empname)
            .input('CLAIMFOR', sql.VarChar(50), claimfor)
            .input('RELATIONSHIP', sql.VarChar(50), forrela) 
            .execute(sqlqry)
    }).then(result => {
        // console.dir(result.recordset)
        callback(result.rowsAffected)
        sql.close()
    }).catch(err => {
        // console.log(err)
        callback(err)
        sql.close()
    })

I need help to understand and assist me on why does this error OK occurs when status code is 200 and server does its part perfect.


Solution

  • HTTP 200 simply means that server is returning okay response. If you wrap an error on server and send it with 200 response, it will show 200 on browser.

    You need to check on the server side before you write the response back, what do you get after your DB query which you are going to return.

    I noticed, when query is executed, in case of error; the code is calling the same callback without separate arguments for error or the result. So it is difficult to find when error occurred. May be use callback of the form

    callback(err, success)
    

    Using this form of callback, you can easily identify the error and success cases in your callback.

    The real check you can do is to console log the object your are sending as response to see what is sent back to the browser.