猫鼬updateOne为什么会引发代理错误?

时间:2019-12-26 14:19:01

标签: node.js reactjs mongoose

我有一个简单的mern应用程序。基本上有一种形式。提交后,将运行后端api,该api将更新mongo db中存在的用户文档。 我遇到的问题是当我编写以下查询时,它引发了一个讨厌的错误无法将请求/ api / auth / fb / register / Patient从localhost:3000代理到http://localhost:5000/

router.post('/auth/fb/register/patient', (req,res) => {
User.updateOne({email: req.body.email}, { role: "patient"})
})

我认为路线错误或其他原因,但是如果我运行查找查询而不是更新查询,则它会完美运行。以下是没有任何错误的查找查询:

router.post('/auth/fb/register/patient', (req,res) => {
  User.findOne({email: req.body.email}).then(user => console.log(user))
})

有人知道为什么更新专门不起作用吗?

1 个答案:

答案 0 :(得分:0)

您必须启用http跨源

     app.use(function(req, res, next) {
      res.header("Access-Control-Allow-Origin", "YOUR-DOMAIN.TLD"); 
      res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, 
      Accept");
      next();
    });

    app.get('/', function(req, res, next) {
     // Handle the get for this route
    });`enter code here`

    app.post('/', function(req, res, next) {
      // Handle the post for this route
    });