簡體   English   中英

無法使用均值堆棧中的findOneAndUpdate更新請求

[英]Unable to update a request using a findOneAndUpdate in Mean Stack

我想使用名為token的字段發布請求。 這是我的貓鼬圖式

const UserSchema = mongoose.Schema({
  name: {
    type: String
  },
  email: {
    type: String,
    required: true,
    unique: true
  },
  username: {
    type: String,
    required: true,
    unique: true
  },
  password: {
    type: String,
    required: true
  },
  phone: {
    type: String
  },

  resetPasswordToken: {
    type: String
  },
  resetPasswordExpires: {
    type: Date
  }

});

這是我來自user.js路線文件

router.put('/reset/:token', function(req, res) {
    console.log('listening');
    User.findOneAndUpdate(
        {resetPasswordToken:req.params.token},
        {
            password: req.body.password,
            resetPasswordToken: undefined,
            resetPasswordExpires: undefined
        },
        function(err,user) {
            if(err) {
                console.log(err + 'is here');
            } else {
                res.json(user);
            }
        }
    );
});

這是我發送`POST請求的地方。

resetPassword(passwordData) {
    let headers = new Headers();
    console.log(passwordData);
    headers.append('Content-Type','application/json');
    return this.http.put('http://localhost:3000/api/reset/'+passwordData.token,passwordData,{headers: headers})
    .map(res => res.json());
  }

沒有任何動作發生,我無法在cmd中看到listening 對於用戶,我已經具有令牌值

"resetPasswordToken" : "2a287acacf7d5e98de9a158c8be82744ef0302f9"

我可以使用郵遞員進行更新,但無法使用代碼進行更新。

如果您能夠通過郵遞員而不是客戶來處理請求,則有兩種可能性:

  • CORS:在處理所有請求之前,請使用以下代碼

    app.use((req,res,next)=>{ res.header("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE"); next() })

  • 使用x-http-method-override標頭:由於有時不允許放置或刪除請求。 這可以在處理請求之前在客戶端或服務器中發送時完成( 單擊此處獲取所使用的節點模塊

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM