0

Server API is Node / Express 4.17, client is Angular 8.

I have a data model in Angular:

export class myData
{ ... }

UI Component .ts uses it:

import { myData } from 'mydata';
...
const md: myData = new myData();
// populate md
this.myService.addData(md);

myservice.ts:

import { myData } from 'mydata';
import { HttpClient, HttpRequest, HttpHeaders } from "@angular/common/http";
...
export class myService{
  constructor(private http: httpClient) {}
  addData(mymd: myData)
  {
    const req = new HttpRequest('POST', url, mymd);
    this.http.request(req).subscribe(something => {});
  }
}

Node/Express server.js:

const express = require('express')
const server = express()
server.post('/api', async function v1(req, res)
{
  let x = req.body;
  ...
}

Fiddler shows md and its content submitted but req has no body, nor md or any user data.

Jeb50
  • 6,272
  • 6
  • 49
  • 87

1 Answers1

0

You need to use body-parser to parse body in Express.

Try to use that.

var express = require('express')
var bodyParser = require('body-parser')

var server = express()

// parse various different custom JSON types as JSON
server.use(bodyParser.json({ type: 'application/*+json' }))

server.post('/api', async function v1(req, res)
{
  let x = req.body;
  ...
}
Rise
  • 1,493
  • 9
  • 17
  • req.body is undefined. – Jeb50 Jan 25 '20 at 22:48
  • 1
    This article may help you in case. https://stackoverflow.com/questions/9177049/express-js-req-body-undefined – Rise Jan 28 '20 at 08:49
  • besides `body-parser`, it's the sequence, your above link solved the last piece of the puzzle. Will be great if you can update your post, I will check it as solution if so. Thank you very much for your help! – Jeb50 Feb 01 '20 at 19:26