This is my function for addEventListener for the click event
export const showTicket = async function() {
try {
// GETTING USER DATA
const response = await fetch("/api/v1/users/me");
const data = await response.json();
//
const user = data.data.data;
// CREATING VISITOR
const visitor = {
VisitorNumber: user.visitorId,
FirstName: user.firstName,
LastName: user.lastName,
Company: user.company,
JobTitle: user.profile,
Email: user.email,
Mobile: user.mobile,
Nationality: user.nationality,
Country: user.country,
Category: user.type,
};
// SENDING VISITOR OBJECT TO ANOTHER API
const url = "/api/v1/eticket/visitor";
const options = {
method: "POST",
header: {
"Content-Type": "application/json",
},
body: JSON.stringify(visitor),
};
if (!user.eventId) {
const response1 = await fetch(url, options);
const data1 = await response1.json();
}
const ticketMarkup = `<div class="ticket">
<div class="ticket__logo"> <img src="/images/vp-expo-logo.png"> </div>
<div class="ticket__data">
<p>Your ticket is ready to be printed</p>
</div>
<button class="btn ticket__print-ticket" data-visitor-id=${user.visitorId}> Print Ticket </button>
<button class="btn update-data"> Update Data </button>
</div>
`;
showPopup(ticketMarkup);
} catch (err) {
console.log(err);
}
};
so first I am getting the user details from the api '/api/v1/users/me' which is working fine and I am able to retrieve the data and the I create a variable 'Visitor' using the user properties.
Then I have to send the visitor variable as a body to another api ('/api/v1/eticket/visitor') so that it can receive the body as req.body
But on the '/api/v1/eticket/visitor' the req.body shows as an empty object.
Here's the opening part of the controller for this receiving api ('/api/v1/eticket/visitor')
exports.addOneVisitor = async function(req, res, next) {
try {
const visitor = req.body;
console.log(req.body);
Please advise what am I doing wrong?
Silliest Mistake Ever!!.. but one I keep repeating all the time. I am answering my own question as a reminder to me (and to anyone out there who makes the same typos repeatedly!)..
It's headers and not header when you write the options part for a fetch request.
Hope this helps someone to not lose 2 days of their life over a missing 's'