放置请求后,Request.body为空对象

时间:2018-12-07 11:32:36

标签: javascript node.js express fetch

我发出放置请求,但我的request.body是空对象

main.js

company = await fetch(`http://localhost:8080/companies/${company.id}`, {
         method: "PUT",
         body: JSON.stringify({ name: "my company" }), 
         headers: { 'Content-Type': 'application/json'} })
         .then(res => res.json()).then(data => data);

company.route.js

this.__router.put('/:id', (req, res) => {
      const { body, params: { id } } = req;
      console.log(body);

      companyController.updateOne(id, body);
      res.status(200).end();
    });

company.controller.js

updateOne(id, {name}) {
    const company = this.findOne(id);

    company.name = name || company.name;
    return company;
  }

app.js

app.use(bodyParser.json());
app.use('/*' , function (req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE");
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  next();
});

app.use(bodyParser.urlencoded())

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

可能是什么问题?

0 个答案:

没有答案