-1

Basically, I am posting id of draft to server to delete draft using fetch and server will response with success text if it succeeds. Here is my code.

fetch(this.url + "/vue/api/processdraft?action=delete&id=" + item.id, {
    method: "POST"
})
    .then(response => {
        return response.text();
    })
    .then(function (response) {
        console.log(response);
        if (response === "success") {
            console.log("Test");
            this.draftList.splice(index, 1);
        }
    });

In above code, in console.log(response) shows success as expected. But code inside if block doesn't work. And I cannot figure out what is wrong with it? It won't even print Test as it is supposed to. Here is screenshot of output.

enter image description here

And I've also checked that there is no white space before or after response text if it matters.

Edit: I checked for whitespaces in serverside and although I made sure there were no whitespace, but forgot to consider newline character. So this've been solved using trim() as suggested by answers.

Kshitij Dhakal
  • 816
  • 12
  • 24
  • 1
    Is there a space in there? Or a newline? – Jonas Wilms Sep 16 '19 at 08:27
  • 1
    Add a breakpont and check the string – adiga Sep 16 '19 at 08:29
  • 2
    `response.trim()` also see https://stackoverflow.com/questions/10805125/how-to-remove-all-line-breaks-from-a-string – kemicofa ghost Sep 16 '19 at 08:29
  • `I've also checked that there is no white space before or after response text if it matters` - how? what is `console.log(response.length)` – Jaromanda X Sep 16 '19 at 08:32
  • Possible duplicate of [How to remove all line breaks from a string](https://stackoverflow.com/questions/10805125/how-to-remove-all-line-breaks-from-a-string) – kemicofa ghost Sep 16 '19 at 08:37
  • 1
    Depending on your needs you could also test for a success status code instead of the text response. `response.status < 400` or of you want to specifically test for a 2xx status simply `response.ok`. See the [Response properties](https://developer.mozilla.org/en-US/docs/Web/API/Response#Properties). However if you need to specifically match body text this might not fit your scenario. – 3limin4t0r Sep 16 '19 at 08:41
  • 2
    fyi, newline characters are also considered whitespace characters. When you say *"I checked for whitespaces"* you meant "I checked for spaces". – 3limin4t0r Sep 16 '19 at 08:47

2 Answers2

1

There might be characters you don't see in the console (newlines, spaces, ...). To show them, you can use JSON.stringify:

    console.log(" success\n"); // success, right?
    console.log(JSON.stringify(" success\n")); // no, not quite ...

To match while ignoring whitespace and newlines, use response.trim() === "success", or response.includes("success").

Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
1

You can try to use tim():

.then(function (response) {
    console.log(response);
    if (response.trim() === "success") {
        console.log("Test");
        this.draftList.splice(index, 1);
    }
});

Hope it'll work.

P4uB0rd4
  • 175
  • 5