简体   繁体   English

如何在mongodb中更新密码值

[英]how to update password value in mongodb

I am writing an API using mongo express and node and I have two collections as of now 1) users 2) userlist 我正在使用mongo express和node编写API,到目前为止我有两个集合1)用户2)用户列表

I am trying to implement change password functionality for the user and want to update the password for the login user.This is my code for the api. 我正在尝试为用户实现更改密码功能,并想更新登录用户的密码。这是我的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);
})

so what I want is in my create user schema my password should get updated when I call app.put api from the front-end. 所以我想要的是在我的创建用户架构中,当我从前端调用app.put api时,我的密码应该会更新。 Please can some-one help by writing the exact query. 请通过编写确切的查询来提供一个帮助。

You will have to have a unique identifier to match to that user object. 您将必须具有唯一的标识符以匹配该用户对象。 Assuming request.body.userEmail is that attribute, it can be like: 假设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");
    } 
  })  
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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