-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path9 Fetch API
More file actions
35 lines (34 loc) · 902 Bytes
/
9 Fetch API
File metadata and controls
35 lines (34 loc) · 902 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//https://www.youtube.com/watch?v=YkFTbnwheVw
//https://www.youtube.com/watch?v=YkFTbnwheVw
const mockFetch = require('./mock-fetch')
const successResponse = [
{
'userId': '1',
"data": 'This looks slick!'
},
{
'userId': '2',
"data": 'I think this can be improved.'
},
{
'userId': '1',
"data": 'What kind of improvement?'
}];
mockFetch('/api/comments', successResponse);
async function getCommentsByUserId(userId) {
var result;
arr=[];
await fetch("/api/comments")
.then(res => res.json())
.then(res =>{
result=res.filter(item => item.userId=userId);
for(var i=0;i<result.length;i++){
arr.push(result[i].data);
}
arr.sort();
return arr;
})
.catch(err => console.log(err));
}
module.exports = getCommentsByUserId;
console.log(getCommentsByUserId(1));