2

I need to receive http status errors with m.request so I use extract as per documentation. But it messes up my data return for some reason.

According to docs, if I use extract to get status then extract return is passed as a parameter to the error callback and data is passed to the success callback. Here is the snippet from docs.

var nonJsonErrors = function(xhr) {
  return xhr.status > 200 ? JSON.stringify(xhr.responseText) : xhr.responseText
}

m.request({method: "GET", url: "/foo/bar.x", extract: nonJsonErrors})
  .then(function(data) {}, function(error) {console.log(error)})

Now, I get status in both success and error callbacks which is wrong. I need to get status on error and data on success. How do I do this? What am I doing wrong? Here is my code:

var Application = {
  run() {
    m.request({
      method: "GET",
      url: "http://localhost:3000/api/session/ping",
      extract(xhr) {return xhr.status;}
    }).then((data) => {
      console.log("Session is Up");
      console.log(data);
      var init = {
        uname: data.uname
      };
      router(init);
    }, (error) => {
      console.log(`Cought: ${error}`);
      m.mount(document.body, Login);
    });
  }
};

Both error and data here give me status codes. I need to get incoming data on success to set up my authentication.

Thanks.

r.sendecky
  • 9,933
  • 9
  • 34
  • 62

1 Answers1

2

OK. I figured it out. I was stupid enough to miss the conditional in the docs snippet that I posted myself. I thought extract returns in case of an error but it returns in both situations and you need to decide yourself within extract definition as to whether return status code or the response body. Got it.

r.sendecky
  • 9,933
  • 9
  • 34
  • 62