I woud like to understand the difference between the two get methods, one work, the other not really but i don t understand why.
This doesn’t work:
JavaScript
x
10
10
1
fetch('https://glo3102lab4.herokuapp.com/fee958c0-c320-40d0-a750-218f2d7c1303/tasks', {
2
method: 'GET',
3
}).then(res => res.json)
4
.catch(error => {
5
console.error('Error:', error);
6
})
7
.then(response => {
8
console.log(response);
9
});
10
and return :
ƒ json() { [native code] }
This works well:
JavaScript
1
8
1
fetch('https://glo3102lab4.herokuapp.com/fee958c0-c320-40d0-a750-218f2d7c1303/tasks').then(function(response){
2
response.json().then(function(data) {
3
console.log(data);
4
});
5
}).catch(function(error) {
6
console.log('Fetch Error:', error);
7
});
8
and return:
{tasks: Array(4)} tasks : (4) [{…}, {…}, {…}, {…}] proto : Object
If you want to try it:
JavaScript
1
19
19
1
fetch('https://glo3102lab4.herokuapp.com/fee958c0-c320-40d0-a750-218f2d7c1303/tasks', {
2
method: 'GET',
3
}).then(res => res.json)
4
.catch(error => {
5
console.error('Error:', error);
6
})
7
.then(response => {
8
console.log("first way");
9
console.log(response);
10
});
11
12
fetch('https://glo3102lab4.herokuapp.com/fee958c0-c320-40d0-a750-218f2d7c1303/tasks').then(function(response){
13
response.json().then(function(data) {
14
console.log("second way");
15
console.log(data);
16
});
17
}).catch(function(error) {
18
console.log('Fetch Error:', error);
19
});
Advertisement
Answer
It doesn’t work because you are returning the res.json
function. You have to call it and return a Promise:
JavaScript
1
2
1
.then(res => res.json())
2