如何在mongodb中更新密码值

时间:2018-04-03 15:00:12

标签: node.js mongodb express mongoose-schema

我正在使用mongo express和node编写API,截至目前我有两个集合 1)用户 2)用户列表

我正在尝试为用户实现更改密码功能,并希望更新登录用户的密码。这是我的api代码。

   const express = require("express");
const app  = express();
const bodyParser  =  require('body-parser');
const port = 4000;
const jwt =  require('jsonwebtoken');  //used to create, sign , and verify tokens
const logger = require('morgan');
const mongoose = require("mongoose");
mongoose.Promise = global.Promise;
mongoose.connect("mongodb://localhost:27017/saddleHorseacademy");

//Setting up basic middleware for all express requests
app.use(logger('dev')); //Log requests to API using morgan

//Enabling CORS from the client side
app.use(function (request,response,next) {
    response.header("Access-Control-Allow-Origin","*");
    response.header('Access-Control-Allow-Methods','PUT ,GET ,POST ,DELETE ,OPTIONS');
    response.header("Access-Control-Allow-Headers","Origin, X-Requested-With, Content-Type, Accept, Authorization, Access-Control-Allow-Credentials");
    response.header("Access-Control-Allow-Credentials","true");
    next();
});

var registerSchema  =  new mongoose.Schema({
    firstName: String,
    lastName: String,
    address: String,
    birthDay: Date,
    packageOption: String,
    batchOption: String,
    startDate: Date,
    endDate: Date,
    phoneNumber: Number,
    emailValue: String,
    specialRemarks: String
});

// var changePassword = new mongoose.Schema({
//     username : String,
//     newPassword : String,
// });

var createUser = new mongoose.Schema({
  userName : String,
  password : String
});

var User = mongoose.model("User",registerSchema);

var userList = mongoose.model("UserList",createUser);

// var passwordChange = mongoose.model("UserList",changePassword);

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));

app.get("/", (request,response) =>
{
  response.send("hello");
});

// post call for creating user
app.post("/addStudent",(request,response)=>{
  var myData = new User(request.body);

  myData.save().then(item=>{
      response.send('Student successfully registerd');
  })
  .catch(error =>{
    response.status(400).send('Student not successfully registered');
  });
});

//updating the password // need help in this block 
app.put("/loginPassword",(request,response)=>{
      var newPassword =  new userList(request.body); 
      userList.findOneAndUpdate()

});
// post call creating user
app.post("/loginPassword",(request,response)=>{
    var myUsers = new userList(request.body);

    myUsers.save().then(item2=>{
      response.send("User Created");
    })
    .catch(error=>{
      response.status(400).send("not able to create user");
    });
});

app.listen(port,()=>{
  console.log("server listening to port" +port);
})

所以我想要的是在我的创建用户架构中,当我从前端调用app.put api时,我的密码应该更新。通过编写确切的查询,请提供一些帮助。

1 个答案:

答案 0 :(得分:0)

您必须拥有与该用户对象匹配的唯一标识符。假设request.body.userEmail是该属性,它可以是:

//updating the password // need help in this block 
app.put("/loginPassword",(request,response)=>{
      var newPassword =  new userList(request.body); 
      userList.findOneAndUpdate({userName: request.body.userEmail}, {$set:{password:newPassword}},{new:false},function(err,doc){
      if(err) {......}
      else {
       response.send("Password Updated");
    } 
  })  
});