I
I
Ingerniated2017-06-01 15:10:14
JavaScript
Ingerniated, 2017-06-01 15:10:14

Why does fetch result in Object and not return result?

Why is it that when I try to display the result data, it is displayed [object Promise], and not the text from json?
And how to get here res.name?

var data = fetch("ajax.json").then(function(res){
  return res.json();
}).then(function(res) {
  return res.name;
}).catch();
document.write(data);

Answer the question

In order to leave comments, you need to log in

4 answer(s)
P
Pavlo Ponomarenko, 2018-09-19
@No_name404

This is where you call the function with no arguments:

function minusGoods(){
  // ......
  showCart();
}

And here you want arguments in it that you pass to JSON.parse
function showCart(data) {
  var pack = JSON.parse(data);

As a result, you get your error:
JSON.parse(undefined)

V
Vladimir, 2018-09-19
@Casufi

See what comes in JSON.parse

D
Deepcat, 2020-02-06
@Deepcat

Look with console.log(data); (put before JSON.parse ) what comes from the handler file, I think you will notice that there is not only json , but "something else". Then go to the handler file and see what sends this "something else". Good luck

A
Alexander Manakov, 2017-06-01
@Ingernirated

Because that's how Promises work.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question