0

The GET method works fine but when i call the POST method on my angular project it seems like the POST function on nodeJs doesn't get called. What am i doing wrong ?

Angular Service:

getAllProducts():  Observable<Product[]> {
return this.http.get<Product[]>('http://localhost:3000/api/content');
}

insertProduct(product: Product): Observable<Product> {
return this.http.post<Product>('http://localhost:3000/api/content', product);
}

server.js:

app.get('/api/content', function (req, res, next) {
    // query to the database and get the records
    mc.query('select * from testContent', function (err, recordset) {

        if (err) console.log(err)

        // send records as a response
        res.send(recordset); 
    });
});


app.post("/api/content",function(req , res, next){

    console.log('I will not get printed');

    mc.query('INSERT INTO testContent (productName,productCode) VALUES (\''+req.body.productName + '\',\'' + req.body.productCode + '\')',function (err, recordset) {

        if (err) console.log(err)

        // send records as a response
        res.send(recordset);

    });

});
1

1 Answer 1

1

Maybe you need a "Content-Type" header in your post request like :

import { HttpClient, HttpHeaders } from '@angular/common/http';

insertProduct(product: Product): Observable<Product> {
  return this.http.post<Product>('http://localhost:3000/api/content', product, {
    headers: new HttpHeaders({ 'Content-Type': 'application/json' })
  });
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.