I am trying to make a post request with Postman. The only thing that returns is the id made by uuidv4() all the other information for the JSON object is not there. I have tried req.body and req.apiGateway.event.body with no luck.
This is my post request:
const serverless = require("serverless-http");
const express = require("express");
const app = express();
// const bodyParser = require("body-parser");
const AWS = require("aws-sdk");
const db = new AWS.DynamoDB.DocumentClient();
const { v4: uuidv4 } = require("uuid");
app.use(express.urlencoded());
app.use(express.json());
// app.use(bodyParser.urlencoded({ extended: true }));
// app.unsubscribe(bodyParser.json());
app.post("/inspections", async (req, res) => {
const data = req.apiGateway.event.body;
const params = {
TableName: "inspectionTrackTable",
Item: {
id: uuidv4(),
unitNum: data.unitNum,
building: data.building,
managed: data.managed,
rental: data.rental,
inHouse: data.inHouse,
robeCount: data.robeCount,
inspected: data.inspected,
notes: data.notes
},
};
try {
await db.put(params).promise();
res.status(201).json({ unit: params.Item });
} catch (e) {
res.status(500).json({ error: e.message });
}
});
This is what I am entering in Postman:
{
"unitNum": "007",
"building": "A",
"managed": true,
"rental": false,
"inHouse": false,
"robCount": 0,
"inspected": false,
"notes": "this is a good unit"
}
I have now tried adding ...req.body to the item like this:
Item: {
id: uuidv4(),
...req.body
},
The response I am getting from that is:
{
"unit": {
"0": 123,
"1": 10,
"2": 32,
"3": 32,
"4": 32,
"5": 32,
"6": 34,
"7": 117,
"8": 110,
"9": 105,
"10": 116,
"11": 78,
"12": 117,
"13": 109,
"14": 34,
"15": 58,
"16": 32,
"17": 34,
"18": 48,
"19": 48,
"20": 55,
"21": 34,
"22": 44,
"23": 10,
"24": 32,
"25": 32,
"26": 32,
"27": 32,
"28": 34,
"29": 98,
"30": 117,
"31": 105,
"32": 108,
"33": 100,
"34": 105,
"35": 110,
"36": 103,
"37": 34,
"38": 58,
"39": 32,
"40": 34,
"41": 65,
"42": 34,
"43": 10,
"44": 32,
"45": 32,
"46": 32,
"47": 32,
"48": 10,
"49": 125,
"id": "efb90ad4-f68e-4ea0-b55c-6a4f79e3a96c"
}
}
I did not expect that... My delete, and get requests are working I am having trouble with the put and post..