【问题标题】:Why does mongoose updateOne throw proxy error?为什么猫鼬 updateOne 会抛出代理错误?
【发布时间】:2019-12-26 22:33:59
【问题描述】:

我有一个简单的 mern 应用程序。基本上有一个表格。提交后,后端 api 运行,更新 mongo db 中存在的用户文档。 我遇到的问题是,当我编写以下查询时,它会抛出一个令人讨厌的错误 Could not proxy request /api/auth/fb/register/patient from localhost:3000 to 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))
})

有谁知道为什么更新特别不起作用?

【问题讨论】:

    标签: node.js reactjs mongoose


    【解决方案1】:

    你必须启用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
        });
    

    【讨论】:

      猜你喜欢
      • 2021-01-13
      • 1970-01-01
      • 1970-01-01
      • 2014-06-19
      • 1970-01-01
      • 2018-12-10
      • 2019-06-13
      • 2021-06-17
      • 2015-02-25
      相关资源
      最近更新 更多