0

I was creating a route that returns me all product names. However with javascript code is written like this:

getSku: function () {
    return $.getJSON(Routing.generate(this.options.urls.get))
        .promise();
},

So I am unable so far to extract the data from that response. If I console log this getSku I get:

{state: ƒ, always: ƒ, then: ƒ, promise: ƒ, pipe: ƒ, …}
always:ƒ ()
done : ƒ ()
fail : ƒ ()
pipe : ƒ ( /* fnDone, fnFail, fnProgress */ )
progress : ƒ ()
promise : ƒ ()
state : ƒ ()
then :ƒ ( /* fnDone, fnFail, fnProgress */ )
__proto__:Object

And when I remove .promise() I get:

{readyState: 1, getResponseHeader: ƒ, getAllResponseHeaders: ƒ, setRequestHeader: ƒ, overrideMimeType: ƒ, …}
abort:ƒ ( statusText )
always:ƒ ()
complete:ƒ ()
done:ƒ ()
error:ƒ ()
fail:ƒ ()
getAllResponseHeaders:ƒ ()
getResponseHeader:ƒ ( key )
overrideMimeType:ƒ ( type )
pipe:ƒ ( /* fnDone, fnFail, fnProgress */ )
progress:ƒ ()
promise:ƒ ()
readyState:4
responseJSON:Array(5)
0:{id: 1, identifier: "test_1", label: "test_1"}
1:{id: 2, identifier: "1000000", label: "1000000"}
2:{id: 3, identifier: "1000001", label: "1000001"}
3:{id: 4, identifier: "1000002", label: "1000002"}
4:{id: 5, identifier: "1000003", label: "1000003"}

And I need somehow to catch that responseJson result. But I don't know how to catch it. It is a backbonejs. I was looking for a various solutions but I always return undefined. Please, help me figure out how to get this json result. Thanks.

0

1 Answer 1

1

Try the following:

getSku: function () {
  return $.getJSON(Routing.generate(this.options.urls.get))
    .promise();
}

When you call getSku() it returns a promise object. You need to wait for the promise to resolve or fail to process the result as follows.

getSku().done((data) => {
   console.log(data);
}).fail(() => {
    console.log( "something went wrong" );
});

To learn more about promises: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, I was using success instead of done, but I will use your version :) Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.