0

My frontend is Angular 5, my backend is Nodejs and my database is SQL Server.

When I test the post with POSTMAN, the backends works fine, adding data in database. But when I try to do it from the Angular app, it returns an error:

{code: "EREQUEST", number: 207, lineNumber: 1, state: 1, class: 16, …}
class
:
16
code
:
"EREQUEST"
lineNumber
:
1
name
:
"RequestError"
number
:
207
originalError
:
info
:
class
:
16
event
:
"errorMessage"
lineNumber
:
1
message
:
"Invalid column name 'NaN'."
name
:
"ERROR"
number
:
207
procName
:
""
serverName
:
"MIDF-C18137"
state
:1

This is my nodejs code

router.post('/clientes', (req, res) =>{
    const dn = parseInt(req.body.dn);
    const entrega = req.body.entrega;
    const dms = req.body.dms;
    const resumo = req.body.resumo;
    execSQLQuery(`INSERT INTO dbcaixa(DN, ENTREGA, DMS, RESUMO) VALUES('${dn}','${entrega}','${dms}','${resumo}')`, res);
})

This is my Angular code

onSubmit(form: any): void {

    var body = 'dn=' + form.dn + '&entrega=' + form.entrega + '&dms=' + form.dms + '&resumo=' + form.resumo;

  this.httpService.post("http://localhost:3000/clientes", body)
            .subscribe(
              data => console.log("success!", data),

               );

    console.log('you submitted value:', body);
  }

The x-www-form-urlencoded I build in var body is EXACLTY the same as the one send by POSTMAN and doesnt work. What am I doing wrong?

Edit: Maybe someone can think it's cors issue. But I already resolve it and I can do get requests

2 Answers2

0

I found the answer to my question in this link How to force Angular2 to POST using x-www-form-urlencoded

Basically, I implemented the code below

let body = new URLSearchParams();
body.set('user', username);
body.set('password', password);

let options = {
    headers: new HttpHeaders().set('Content-Type', 'application/x-www-form-urlencoded')
};

this.http
    .post('//yourUrl.com/login', body.toString(), options)
    .subscribe(response => {
        //...
    });
0

Check this out for Angular 5 with HttpClient library.

Mnemo
  • 652
  • 1
  • 13
  • 18