i am trying to make a authentication user part by nodejs/expressjs with mongodb where users will have different roles and the parameters of the users will be saved in mongodb. in case of signing in for every user, they will be saved as "user" by default. after edit, its role will change into admin or moderator and this role change will be updated in mongodb also.
here is my userschema in node.js/Express.js:
const mongoose = require("mongoose");
const userSchema = new mongoose.Schema({
username: {
type: String,
required: true,
},
email: {
type: String,
required: true,
unique: true,
},
password: {
type: String,
required: true,
minlength: 8,
},
displayName: {
type: String,
},
role: {
type: String,
enum: ['user', 'moderator', 'admin'],
default: 'user',
},
resetLink: {
data: String,
default: "",
},
});
module.exports=User=mongoose.model("user",userSchema)
here is router.put for editing and updating the role:
router.put("/:username/newrole",async(req,res)=>{
let role,username;
try {
username = req.params.username;
console.log(username);
const result = await User.updateOne(
{ username: req.body.username },
{ $set: { role: req.body.role } }
);
console.log("result = ", result);
res.status(200).json({ msg: "User role has been updated successfully!" });
} catch(e) {
if (User == null) {
console.log(e)
res.status(400).json({ msg: "no such username found!" });
} else {
User: User,
console.log(e);
res.status(405).json({ msg: "Error updating!" });
}
}
})
i am using the postman to check the code. the url for the edit is http://localhost:5000/users/admin/newrole where admin is the username of the user which role is going to be changed. and in the body row of the post i am giving the input as below:
{
"role":"user"
}
but output is showing successfuly user role changed but the console.log(result):
result = {
n: 0,
nModified: 0,
opTime: {
ts: Timestamp { _bsontype: 'Timestamp', low_: 2, high_: 1611010685 },
t: 7
},
electionId: 7fffffff0000000000000007,
ok: 1,
'$clusterTime': {
clusterTime: Timestamp { _bsontype: 'Timestamp', low_: 2, high_: 1611010685 },
signature: { hash: [Binary], keyId: [Long] }
},
operationTime: Timestamp { _bsontype: 'Timestamp', low_: 2, high_: 1611010685 }
}
the databse user role is showing role: null which is suppose to be changing into "user". where did i make the mistake?
please let me know
finally solved it. here is the following code for update the role:
router.patch('/:id/update',async(req,res)=>{
try {
const user=await User.updateOne({_id:req.params.id},{$set:{role:req.body.role}},{
new:true
})
console.log(req.body.role);
} catch (e) {
console.log(e)
}
})
in postman i had to use x-www-form-urlencoded where i have selected key as role and value is admin or user or whatever i want acording to enum. and the request url must contain the _id number as i have filtered by it and it would be a patch request.
that has done the trick.
but thanx J.F. for help. your chat helped me to understand about mongoose and mongodb commands and nodejs/expressjs handling the route.