无法从前端向后端发送有效负载

时间:2016-11-13 20:47:20

标签: javascript node.js http express sequelize.js

我正在制作一个聊天机器人,人们每30分钟就会获得一次硬币,然后他们可以花这些钱币。当他们花这些钱币时,他们需要扣除,所以我需要在我的前端抓住他们的命令并从我的后端的个人资料中扣除硬币。

这是我的切换案例,我抓住了正在使用的命令

case `!gayOverlay`:
        const requiredKluiten = 50;

        fetch(`/api/users/${userstate.username}`) // fetch from Express.js server
          .then(response => response.json())
          .then(result => {
            if (result.instakluiten >= requiredKluiten) {
              client.action(channel, `${userstate[`display-name`]}, you've got enough instakluiten for this.`);

              const method = `PATCH`;
              const payload = {requiredKluiten};
              const body = JSON.stringify(payload);
              const headers = `Content-Type: application/json`;

              return fetch(`/api/users/${userstate.username}`, {method, body, headers})
                .then(r => console.log(`Result is `, d));
            }else{
              client.action(channel, `${userstate[`display-name`]}, you don't have enough instakluiten for this, you need ${requiredKluiten - result.instakluiten} more.`);
            }
          });
        break;

我正在使用PATCH来更新我的观看者的硬币(我读过Please do not patch like an idiot,但评论中的人也反对这个方法,所以我不确定现在的正确方法是什么...... ),当我用邮递员发送数据时,我的后端的req.body充满了我添加的数据。但是,当我执行我的函数(由于我的有效负载也应该有数据)时,我的req.body是一个空对象。我在这里做错了什么?

这就是服务器端发生的事情

app.use(bodyParser.urlencoded({ extended: true }));

app.get('/api/users/:username', (req, res) => {
  let username = req.params.username;
  findUserInApi(username)
    .then(foundUser => {
      console.log(`foundUser is ${foundUser}`);
      if (!foundUser) {
        res.status(403).send('User not found in api yet');
      }else{
        res.json(foundUser);
      }
    });
});

更新

这是在我的补丁之后返回的内容(在Alex的评论之后将其更改为PUT)https://puu.sh/shO57/362b639545.png

更新

即使这样尝试(因为this post)也没有区别

const data = JSON.stringify(payload);
const body = new FormData();
body.append(`json`, data);

更新

当使用邮递员时,我的身体总是:{'requiredKluiten':50}。

当我删除标题(在选择x-www-form-urlencoded以发送我的数据时自动设置为application / x-www-form-urlencoded)时,我会使用Postman获取一个空对象。当使用x-www-form-urlencoded再次添加标题时,它可以正常工作。当我选择表格数据时,我得到了req.body:

{ '------WebKitFormBoundarySAXAR2jfhUJt39US\r\nContent-Disposition: form-data; name': '"requiredKluiten"\r\n\r\n50\r\n------WebKitFormBoundarySAXAR2jfhUJt39US--\r\n' }

然而,当我将代码添加到我的代码中时,在执行我的提取时它仍然无效...我仍然必须做错事:/ ...

这就是现在正在执行的任何缺陷?

case `!gayOverlay`:
const requiredKluiten = 50;

  fetch(`/api/users/${userstate.username}`) // fetch from Express.js server
          .then(response => response.json())
          .then(result => {
            if (result.instakluiten >= requiredKluiten) {
              client.action(channel, `${userstate[`display-name`]}, you've got enough instakluiten for this.`);

              const method = `PUT`;
              const payload = {requiredKluiten};
              const body = JSON.stringify(payload);
              const headers = `Content-Type: application/x-www-form-urlencoded`;

              return fetch(`/api/users/${userstate.username}`, {method, body, headers})
                .then(d => console.log(`Result is `, d));
            }else{
              client.action(channel, `${userstate[`display-name`]}, you don't have enough instakluiten for this, you need ${requiredKluiten - result.instakluiten} more.`);
            }
          });

0 个答案:

没有答案