简体   繁体   English

通过 fetch 使用 post 方法发送数据返回一个空对象

[英]Sending data using post method via fetch returns an empty object

This is my function for addEventListener for the click event这是我用于click事件的addEventListener函数

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.所以首先我从 api '/api/v1/users/me' 获取用户详细信息,它工作正常,我能够检索数据,并使用用户属性创建一个变量 'Visitor'。

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然后我必须将访问者变量作为正文发送到另一个 api ('/api/v1/eticket/visitor') 以便它可以接收正文作为 req.body

But on the '/api/v1/eticket/visitor' the req.body shows as an empty object.但是在“/api/v1/eticket/visitor”上,req.body 显示为一个空对象。

Here's the opening part of the controller for this receiving api ('/api/v1/eticket/visitor')这是这个接收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.当您为获取请求编写选项部分时,它是headers而不是header

Hope this helps someone to not lose 2 days of their life over a missing 's'希望这可以帮助某人不要因为缺少“s”而失去 2 天的生命

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM