简体   繁体   中英

Unable to update a request using a findOneAndUpdate in Mean Stack

I want to post a request in using a field called token . This is my mongoose schema

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
  }

});

This is my routes file from 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);
            }
        }
    );
});

This is from where I'm sending `POST request.

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());
  }

No action is getting taking place I'm unable to see the listening in my cmd. For a user I already have token value as

"resetPasswordToken" : "2a287acacf7d5e98de9a158c8be82744ef0302f9"

I'm able to update using postman but unable to update with code.

If you are able to process the requests by postman and not by client there are 2 possibilities:

  • CORS: Use the following code, before processing all your requests

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

  • Use x-http-method-override header: As put or delete requests sometimes are not allowed. This can be done while sending in the client or in the server before processing the request ( click here for the node module used )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM