我的 API 为什么不能按预期工作?错误:无效的请求。

huangapple go评论73阅读模式
英文:

Why my api is not working as expected? error Invalid request

问题

Your API is not working as expected because it's returning an "Invalid request" error when certain conditions are not met. Here's the translated code snippet:

后端

router.post('/save', (req, res) => {
  if (!req.user || !req.user.email || !req.body || !req.body.city) {
    return res.status(400).json({ error: 'Invalid request' });
  }

  const city = req.body.city;
  const email = req.user.email;

  User.findOneAndUpdate({ email: email }, { city: city }, (err, user) => {
    if (err) {
      return res.status(500).json({ error: err.message });
    }
    res.json({ message: 'City saved successfully' });
  });
});

我在POST请求中这样做

{
  "email": "myemail@gmail.com",
  "city": "New York"
}

也尝试了这个POST请求

{
  "city": "New York"
}

Please make sure you provide the "email" and "

英文:

Why my api is not working as expected? error Invalid request i am testing my api in postman software but its not working its showing me error Invalid request can anyone tell whats the problem?

Backend:

router.post('/save', (req, res) => {
  if (!req.user || !req.user.email || !req.body || !req.body.city) {
    return res.status(400).json({ error: 'Invalid request' });
  }

  const city = req.body.city;
  const email = req.user.email;

  User.findOneAndUpdate({ email: email }, { city: city }, (err, user) => {
    if (err) {
      return res.status(500).json({ error: err.message });
    }
    res.json({ message: 'City saved successfully' });
  });
});

I am doing like this on post req:

{
  "email": "myemail@gmail.com",
  "city": "New York"
}

And also tried this on post req:

{
  "city": "New York"
}

答案1

得分: 1

以下是您要翻译的内容:

"我认为这是因为您的条件。req.user和req.user.email可能是用于身份验证中间件。您必须在路由上添加中间件或使用以下方法--->

router.post('/save', (req, res) => {
  if (!req.body || !req.body.city) {
    return res.status(400).json({ error: 'Invalid request' });
  }

  const city = req.body.city;
  const email = req.user.email;

  User.findOneAndUpdate({ email: email }, { city: city }, (err, user) => {
    if (err) {
      return res.status(500).json({ error: err.message });
    }
    res.json({ message: 'City saved successfully' });
  });
});
英文:

I think it is because of your condition . req.user & req.user.email maybe for authenticated middleware . you must add middleware to your route or using this --->

router.post('/save', (req, res) => {
  if (!req.body || !req.body.city) {
    return res.status(400).json({ error: 'Invalid request' });
  }

  const city = req.body.city;
  const email = req.user.email;

  User.findOneAndUpdate({ email: email }, { city: city }, (err, user) => {
    if (err) {
      return res.status(500).json({ error: err.message });
    }
    res.json({ message: 'City saved successfully' });
  });
});

huangapple
  • 本文由 发表于 2023年1月3日 17:38:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/74991476.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定