-2

I am sometimes getting error with this code:

// Check if sent user properties are valid
user.validate((err) => {
  if (err) {
    res.status(400).json({
      error: 'User data not valid.',
    });
  }
});

// Check if sent password properties are valid
password.validate((err) => {
  if (err || !password.arePasswordsEqual(req.body.password, req.body.passwordConfirm)) {
    res.status(400).json({
      error: 'Password data not valid.',
    });
  }
});

Any help appreciated.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
be-codified
  • 5,704
  • 18
  • 41
  • 65
  • 1
    please provide a [mcve] – Daniel A. White Jan 12 '18 at 16:51
  • We need to see the entire request handler or middleware handler that causes this error. This is not enough code to show the source of the problem. – jfriend00 Jan 12 '18 at 16:59
  • There are several hundred other questions here reporting the exact same error. If you're not going to provide us a minimal, complete and verifiable example (e.g. enough code for us to see the whole problem here), then just go read a few of the other duplicate questions and get your solution from that. – jfriend00 Jan 12 '18 at 17:15

2 Answers2

1

The error you are talking about is occur when you send response multiple time back .

If this is your whole block then see you are sending two response back one is inside user.validate function and other in password.validate .

// Check if sent user properties are valid
user.validate((err) => {
  if (err) {
    res.status(400).json({
      error: 'User data not valid.',
    });
  }
});

// Check if sent password properties are valid
password.validate((err) => {
  if (err || !password.arePasswordsEqual(req.body.password, req.body.passwordConfirm)) {
    res.status(400).json({
      error: 'Password data not valid.',
    });
  }
});

May be you can do like this

   // Check if sent user properties are valid
user.validate((err) => {
  if (err) {
    res.status(400).json({
      error: 'User data not valid.',
    });
  }else{
     // Check if sent password properties are valid
     password.validate((err) => {
       if (err || !password.arePasswordsEqual(req.body.password, req.body.passwordConfirm)) {
         res.status(400).json({
           error: 'Password data not valid.',
         });
       }
     });

  }
});

This will help you .

Himanshu sharma
  • 7,487
  • 4
  • 42
  • 75
0

I believe res.status will already start sending response headers. Even if it doesn't, res.json definitely will. That is to say, you can't call res.status or res.json twice which will happen in the above code if both the user and password are invalid.

Instead, if you want to have multiple errors at once you can collect them in an object and build the response once:

const errors = [];
user.validate((err) => {
  if (err) {
    errors.push[err];
  }
});

// Check if sent password properties are valid
password.validate((err) => {
  if (err || !password.arePasswordsEqual(req.body.password, req.body.passwordConfirm)) {
    errors.push('Password data not valid.');
  }
});

if (errors.length) {
  res.status(400).json(errors);
} else { /* 200 case? */ }

Note that if .validate is asynchronous you will have to do something to synchronize the code above.

Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
  • 1
    @jfriend00 I don't think that this user is looking for a reward; I think they are looking for insight to the issue they are experiencing. Please downvote, close vote, or comment on the question to inform the user that you believe their question is incomplete. – Explosion Pills Jan 12 '18 at 17:06
  • @jfriend00 I think we have differing political views about Stackoverflow. A lot of people use it as a resource to help with their work. We can simultaneously help them improve their question-asking, which is important to you, and help them solve their everyday problems, which is important to me. I don't think they are competing goals, and while I don't think the question is "crummy," I agree it could be more complete. If good answer to bad questions were discouraged, then badges like reversal would not exist https://stackoverflow.com/help/badges/95/reversal – Explosion Pills Jan 12 '18 at 17:19
  • So, did this OP learn anything here about how to write a better question? It doesn't appear so. They wrote a sub-standard, incomplete question, got their answer and left and, in the process, didn't leave a particularly good reference going forward either. With this level of detail in the question, it should probably just be marked a duplicate of one of the hundred other questions on the same topic. – jfriend00 Jan 12 '18 at 17:45