Req.Body is undefined in Node but Data visible in JSON

Using the framework of the “DoneWithIt” app, I have renamed “listings” to “profiles”. When attempting to create a new Profile (aka listing), the server returns a 200 response, and the default details in the Mongoose schema are visible in the database, but not the user input data, name, age and categoryID. In the console, the below details are returned. The FormData appears to be in “data”, and because of that extra layer, maybe that’s why it’s not saving? I’ve spent quite some time troubleshooting online. I’m using “app.use(express.json());” and “app.use(express.urlencoded({ extended: true }));” as body-parser is deprecated. Not sure what else to try. All works as expected when using Postman.

Console Log

Object {
  "config": Object {
    "adapter": [Function xhrAdapter],
    "baseURL": "http://192.168.1.204:3000/api",
    "data": FormData {
      "_parts": Array [
        Array [
          "name",
          "Thanks ",
        ],
        Array [
          "age",
          "22",
        ],
        Array [
          "categoryId",
          4,
        ],
        Array [
          "description",
          "",
        ],
        Array [
          "location",
          "{\"latitude\":xx.xxxx,\"longitude\":-xx.xxxx}",
        ],
      ],
    },
    "headers": Object {
      "Accept": "application/json",
      "x-auth-token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.xxxx",
    },
    "maxBodyLength": -1,
    "maxContentLength": -1,
    "method": "post",
    "onUploadProgress": [Function onUploadProgress],
    "timeout": 0,
    "transformRequest": Array [
      [Function transformRequest],
    ],
    "transformResponse": Array [
      [Function transformResponse],
    ],
    "url": "/profiles",
    "validateStatus": [Function validateStatus],
    "xsrfCookieName": "XSRF-TOKEN",
    "xsrfHeaderName": "X-XSRF-TOKEN",
  },
  "data": Object {
    "__v": 0,
    "_id": "60cfd63794c41bd6ba22a612",
    "date": "2021-06-20T23:58:47.033Z",
    "hashtags": Array [],
    "images": Array [],
    "lastLogin": "2021-06-20T23:58:47.033Z",
    "location": Array [],
    "tags": Array [],
  },
  "duration": 123,
  "headers": Object {
    "connection": "keep-alive",
    "content-length": "165",
    "content-security-policy": "default-src 'self';base-uri 'self';block-all-mixed-content;font-src 'self' https: data:;frame-ancestors 'self';img-src 'self' data:;object-src 'none';script-src 'self';script-src-attr 'none';style-src 'self' https: 'unsafe-inline';upgrade-insecure-requests",
    "content-type": "application/json; charset=utf-8",
    "date": "Sun, 20 Jun 2021 23:58:47 GMT",
    "etag": "W/\"a5-UJXexucjFxLJR0cdm7O2Hf1r6Z4\"",
    "expect-ct": "max-age=0",
    "keep-alive": "timeout=5",
    "referrer-policy": "no-referrer",
    "strict-transport-security": "max-age=15552000; includeSubDomains",
    "x-content-type-options": "nosniff",
    "x-dns-prefetch-control": "off",
    "x-download-options": "noopen",
    "x-frame-options": "SAMEORIGIN",
    "x-permitted-cross-domain-policies": "none",
    "x-xss-protection": "0",
  },
  "ok": true,
  "originalError": null,
  "problem": null,
  "status": 200,
}

Routes > Post

const express = require('express');
const router = express.Router();
const auth = require('../middleware/auth');
const {Profile} = require('../models/profile');

router.post('/', auth, async (req, res) => {
    let profile = new Profile({
        age: req.body.age,
        categoryId: req.body.categoryId,
        name: req.body.name
    });

    profile = await profile.save();

    res.send(profile);
});

module.exports = router;

Mongoose Schema

const mongoose = require('mongoose');

const Profile = mongoose.model('Profile', new mongoose.Schema({
    age: { type: Number, min: 18, max: 99 },
    categoryId: Number,
    date: { type: Date, default: Date.now },
    description: { type: String, maxlength: 250 },
    hashtags: [ String ],
    images: [ { String } ],
    lastLogin: { type: Date, default: Date.now },
    location: [ { String } ],
    name: { type: String, maxlength: 15 },
    tags: [ String ],
}));

exports.Profile = Profile;

App Code

import client from "./client";

//const endpoint = "/listings";
const endpoint = "/profiles";

const getProfiles = () => client.get(endpoint);

export const addProfile = (profile, onUploadProgress) => {
    const data = new FormData();
        data.append('name', profile.name);
        data.append('age', profile.age);
        data.append('categoryId', profile.category.value);
        data.append('description', profile.description);

    profile.images.forEach((image, index) =>
        data.append('images', {
            name: 'image' + index,
            type: 'image/jpeg',
            uri: image
        }));

    if (profile.location)
    data.append('location', JSON.stringify(profile.location));

    return client.post(endpoint, data, {
        onUploadProgress: (progress) =>
            onUploadProgress(progress.loaded / progress.total),
    });
}

export default {
    addProfile,
    getProfiles,
};

Ok, so the issue was that I needed to implement multer. All is well now.