I've just managed to get my $http post working over Angular.where the admin only can make edit for data such gives permission to user to be admin or block user and update the user's description. I write every thing I learnt, but it gives me the below error when I post the data it gives this error in my Command prompt : errors: { password: { MongooseError: Password should contain #$@! upper and lower case and should be between 6 and 8 characters at ValidatorError (C:\jobapp\node_modules\mongoose\lib\error\validator.js:24:11) at validate (C:\jobapp\node_modules\mongoose\lib\schematype.js:732:13) at C:\jobapp\node_modules\mongoose\lib\schematype.js:802:5 at model.validator (C:\jobapp\node_modules\mongoose-validator\lib\mongoose-validator.js:55:16) at asyncValidate (C:\jobapp\node_modules\mongoose\lib\schematype.js:791:29) at deprecated (internal/util.js:41:15) at C:\jobapp\node_modules\mongoose\lib\schematype.js:760:9 at Array.forEach (native) at SchemaString.SchemaType.doValidate (C:\jobapp\node_modules\mongoose\lib\schematype.js:738:19) at C:\jobapp\node_modules\mongoose\lib\document.js:1479:9 at _combinedTickCallback (internal/process/next_tick.js:73:7) at process._tickCallback (internal/process/next_tick.js:104:9) message: 'Password should contain #$@! upper and lower case and should be between 6 and 8 characters', name: 'ValidatorError', properties: [Object], kind: 'user defined', path: 'password', value: '$2a$10$cNJ6jAYs8bLZy5qE4RSMoeKbvz1PFZPRkK8T1ODxnD5QNRFxCrH5K', reason: undefined } }, message: 'Users validation failed', name: 'ValidationError' } { MongooseError: Users validation failed at ValidationError }
When I refresh the page I got this error in console: angular.js:14525 Possibly unhandled rejection: {"data":null,"status":-1,"config":{"method":"PUT","transformRequest":[null],"transformResponse":[null],"jsonpCallbackParam":"callback","url":"/sign/edit/","data":{"_id":"590470c0d9f3c1187002431c","aboutme":"dsdjksjkdjk"},"headers":{"Accept":"application/json, text/plain, /","Content-Type":"application/json;charset=utf-8","x-access-token" My code in my server call in Express.js to my MongoDB:
//insert new value
rout.put('/edit', function(req, res){
var editUser = req.body._id;
if(req.body.permission) var newPermission = req.body.permission;
if(req.body.block) var newBlock = req.body.block;
if(req.body.aboutme) var newAbout = req.body.aboutme;
User.findOne({username: req.decoded.username},function(err,mainUser){
if(err) throw err;
if(!mainUser){
res.json({success:false, message: 'no User found'});
}else{
if(newAbout){
if(mainUser.permission === 'admin'){
User.findOne({_id: editUser},function(err, user){
if(!user){
res.json({success:false, message: 'Now user found'});
} else{
user.aboutme = newAbout;
user.save(function(err){
if(err){
console.log(err);
} else{
res.json({success:true, message : 'Description about user has been Update'});
}
});
}
});
}else{
res.json({success:true, message:'Insufficient Permissions'});
}
}
if(newBlock){
if(mainUser.permission === 'admin'){
User.findOne({_id: editUser},function(err, user){
if(!user){
res.json({success:false, message: 'Now user found'});
} else{
user.block = newBlock;
user.save(function(err){
if(err){
console.log(err);
} else{
res.json({success:true, message : 'Successfully you blocked a user'});
}
});
}
});
}else{
res.json({success:true, message:'Insufficient Permissions'});
}
}
//here I used a lot if condition just to provide high security
if(newPermission) {
if(mainUser.permission === 'admin'){
User.findOne({_id: editUser},function(err, user){
if (err) throw err;
if(!user){
if(newPermission === 'user'){
if(user.permission === 'admin'){
if(mainUser.permission !== 'admin'){
res.json({success:false, message: 'Insufficient Permissions. You must be an admin to downgrade'});
} else{
user.permission = newPermission;
user.save(function(err){
if(err){
console.log(err);
}else{
res.json({success:true, message: 'Permissions have been Update'});
}
});
}
}
if(newPermission === 'admin'){
if (mainUser.permission === 'admin'){
user.permission = newPermission;
user.save(function(err){
if(err){
console.log(err);
}
else {
res.json({success:true, message:'Permissions have been Update'});
}
});
} else {
res.json({success:false, message:'Insufficient Permissions have been updated!!'});
}
}
}
}
});
}
else {
res.json({success:true, message:'Insufficient Permissions'});
}
}
}
});
});
in controller (This is my Angular post call:)
app.UpdateUsername = function(newAbout, valid){
app.errMsg = false;
app.disabled = true;
var userObject = {}; // Create a user object to pass to function
userObject._id = app.currentUser; // Get _id to search database
if(valid){
userObject.aboutme = $scope.newAbout; // Set the new name to the user
// Runs function to update the user's name
User.adminEditUser(userObject).then(function(data) {
// Check if able to edit the user's name
if (data.data.success) {
// $scope.alert = 'alert alert-success'; // Set class for message
app.successMsg = data.data.message;
// Set success message
// Function: After two seconds, clear and re-enable
$timeout(function() {
app.nameForm.aboutme.$setPristine(); // Reset name form
app.nameForm.aboutme.$setUntouched(); // Reset name form
app.successMsg = false; // Clear success message
app.disabled = false; // Enable form for editing
}, 2000);
} else {
//$scope.alert = 'alert alert-danger'; // Set class for message
app.errorMsg = data.data.message; // Clear any error messages
app.disabled = false; // Enable form for editing
}
});;
} else{
app.errorMsg = 'Please Ensure you filled form properly';
app.disabled = false
}
}
})
Html page:
<form name="edit.nameForm" ng-show="edit.phase1" novalidate ng-submit="edit.UpdateUsername(newAbout, edit.nameForm.aboutme.$valid)">
<!--Username-->
<div class="col-md-12">
<div class="form-group">
<div ng-class="{ 'has-success':(edit.nameForm.aboutme.$valid && !edit.nameForm.aboutme.$pristine), 'has-error':(!edit.nameForm.aboutme.$valid && !edit.nameForm.aboutme.$pristine) || (!edit.nameForm.aboutme.$valid && edit.nameForm.$submitted) }">
<label class="label label-primary">About User</label>
<br>
<input ng-disabled="edit.disabled" class="form-control" type="text" id="test_id" name="aboutme" placeholder="Please Write 100 words about Youeself " ng-model="newAbout" ng-pattern="/^[A-Za-z ]+$/" ng-minlength="0" ng-maxlength="100" required/>
<p class="help-block" ng-show="(!edit.nameForm.aboutme.$pristine && edit.nameForm.aboutme.$error.required) ||
(edit.nameForm.$submitted && edit.nameForm.aboutme.$error.required)">This field is required</p>
<ul ng-show="(!edit.nameForm.aboutme.$pristine && edit.nameForm.aboutme.$error.pattern) ||
(!edit.nameForm.aboutme.$pristine && edit.nameForm.aboutme.$error.minlength) || (!edit.nameForm.aboutme.$pristine && edit.nameForm.aboutme.$error.maxlength)" class="help-block">
<li>Must not contain any numbers or special characters</li>
<li>You must write 100 words no more</li>
</ul>
</div>
</div>
</div>
<center>
<h5><button class="btn btn-primary btn-block" type="submit">Update</button></h5></center>
</div>
</form>
<div class="row show-hide-message" ng-show="edit.successMsg">
<div class="alert alert-success"> </div>
</div>
<br>
<div class="row show-hide-message" ng-show="edit.errorMsg">
<div class="alert alert-danger"></div>
</div>
is service:
userFactory.adminEditUser = function(id) {
return $http.put('/sign/edit/', id);
}
More than two days I stuck up in these error So Does anyone have any idea why this might be happening? and what I must do? I am sure there is mistake but I don't know where
via Ala Java
No comments:
Post a Comment