-1

Could someone take a look at this and help me out? I've been working on it for a little while now and can't figure out what I'm missing.

I'm trying to set the inner HTML of a DOM element based upon the presence of a code within the message value of this response object

{
  error: "{\"code\":-1121,\"msg\":\"Invalid symbol.\"}"
  message: "400 - \"{\\\"code\\\":-1121,\\\"msg\\\":\\\"Invalid symbol.\\\"}\""
  name: "StatusCodeError"
  statusCode: 400
}

const data = await res.json();
if (data.statusCode == 400) {
  let parsedData = data.toString();
  if (parsedData.includes('1121')) {
    statusMessage.className = 'alert alert-danger text-center err-message';
    statusMessage.innerHTML = 'Invalid symbol.'

  }
}
Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
  • What is the problem? – VLAZ May 12 '21 at 15:38
  • 2
    A much easier way to check this is to say `if (data.error.code === 1121) { /* do stuff */} ` – mhodges May 12 '21 at 15:40
  • @VLAZ the statusMessage element isn't updating although the condition is met but instead defaulting to the else statement (which I forgot to include in the snippet). Regardless, I'm not sure I am using the includes method correctly. – user2079164 May 12 '21 at 15:44
  • @user2079164 "*although the condition is met*" how do you know it is? Have you tried logging anything? Or debugging? [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173) – VLAZ May 12 '21 at 15:45
  • Does mhodges' comment work for you? – adiga May 12 '21 at 15:45
  • @user2079164 Please [edit] your question to include a full explanation of the problem. – Heretic Monkey May 12 '21 at 15:45
  • 1
    @mhodges `data.error.code` is undefined because `data.error` is a string. – Heretic Monkey May 12 '21 at 15:47
  • @Heretic Monkey `JSON.parse(data.error)` – mhodges May 12 '21 at 15:53
  • @VLAZ I know it _isn't_ being met because otherwise I wouldn't be here asking for help. I will try to semantically align my comments closer to your preference in the future. Thanks for the link. – user2079164 May 12 '21 at 16:13
  • @user2079164 the condition being met and an update not happening is *different* to a condition not being met therefore no update is happening. The first means that the `if` check succeeds and the code inside it doesn't work. The second means the `if` check doesn't succeed, so it's the check that fails. Crucial difference. With this in mind *basic debugging* would be to check why `parsedData.includes('1121')` produces unexpected result. Which would lead to checking `parsedData` and the problem becomes immediately apparent. Even a simple `console.log(parsedData)` would have sufficed. – VLAZ May 12 '21 at 16:20

3 Answers3

1

data.error is JSON, you should parse it and then check the code property.

const data = await res.json();
if (data.statusCode == 400) {
  let error = JSON.parse(data.error);
  if (error.code == -1121) {
    statusMessage.className = 'alert alert-danger text-center err-message';
    statusMessage.innerHTML = error.msg;

  }
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

Before you parsed the data transform it to JSON format.

let parsedData = JSON.parse(data.error);

Check for a specific property code by using dot notation

if(parsedData.code === -1121))
XMehdi01
  • 5,538
  • 2
  • 10
  • 34
-1

The parsedData checked should be the message not the whole data

var data={
  error: "{\"code\":-1121,\"msg\":\"Invalid symbol.\"}",
  message: "400 - \"{\\\"code\\\":-1121,\\\"msg\\\":\\\"Invalid symbol.\\\"}\"",
  name: "StatusCodeError",
  statusCode: 400,
}


if (data.statusCode == 400) {
  let parsedData = data.message.toString();
  if (parsedData.includes('1121')) {
   console.log('alert alert-danger text-center err-message');
    console.log('Invalid symbol.')}
  }