Link to home
Start Free TrialLog in
Avatar of Bruce Gust
Bruce GustFlag for United States of America

asked on

How can I display an error message in this code?

Here's a screenshot of the page I'm working on:

User generated image
The thing that I'm targeting is what you see in the big, black box - "An unexpected error was encountered while trying to process your request. Please try again."

That message is being generated as a result of the "submit" function you see on "profile.js" -  this is a stripped down version...

router.post('/profile/save', async (req, res) => {
  try {
    if (typeof req.body.user == 'undefined') {
      return res.send(user.error('Invalid request.'));
      }
    	
	//checking to ensure current password matches what's in the database before updating profile information
	let check = await(user.getOne(global._user._id));

	let the_password=check.password;
	let the_current_password = req.body.current_password;
	let quiz = await(user.confirmPassword(the_current_password, the_password));
	if(quiz=="yes") {
		console.log("book");
                //here's where my update / save functionality will occur
	}
	else {
		//what do I need to do here?
	}

  } catch (err) {
    res.status(500).send(user.error());
  }

});

Open in new window


"profile.html.twig" is the page the user is actually engaging in order to alter their password etc. It references "profile.js" (the code that you see above" as well as "user-form.js." "user-form.js" is being used to validate the form data as well as produce the "An unexpected error was encountered while trying to process your request. Please try again." error if something goes south with the "save / update" functionality - at least that's what I'm thinking.

Here's my dilemma.

I want to change the text in the message that you see in the screenshot above to "Be sure the password you've entered in the "Confirm Password" field matches what's in the database."

How?

It would see that "user-form.js" is convinced that there's an error that justifies the "An unexpected error..." message.

Why?

I don't understand why on "profile.js" the code is allowed to proceed to the point where I'm sending the "user-form.js" file a 500 status.

Secondly, what changes do I have to make in order to alter the "An unexpected error..." text to "Your 'confirm password' entry doesn't match what's in the database."?

Thanks!
ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Bruce Gust

ASKER

Chris!

I got it figured out!

This was my "else" statement in the event of a user having entered an inaccurate password:

let resp="Confirm your current password is correct!"; //if you don't have a match, send a message to the "submit" command on "user-form.js"
                  return res.send(resp);

That was then picked up by the "user-form.js" form and I rendered things like this:

.fail(function(resp){
      if(resp="Confirm your current password is correct!") {
            clearMsg();
            addMsg('Confirm your current password is correct.');
      }
      else {
      clearMsg();
      addMsg('An unexpected error was encountered while trying to process your request. Please try again.');
      }
}).always(function(){
  $btn.html(html);
});
});

... and that got it done!

Thanks!