I have a database and I am trying to sort through the data in the database and find the one that has a name property equal to the id variable that I specified. The id variable works though I am having trouble sorting through and finding the name property equal to the id variable. This what I have tried:
JavaScript
x
62
62
1
import {useParams} from "react-router-dom";
2
3
function StudentInfo() {
4
5
const {id} = useParams();
6
var studentData = {};
7
8
function infoGetter() {
9
10
fetch(
11
"{url that I put in real code}"
12
)
13
.then(response => response.json)
14
.then(data => {
15
16
Object.keys(data).filter((element) => {
17
18
if (data[element].studentName.toLowerCase() === id.toLowerCase()) {
19
studentData = {
20
studentName: data[element].studentName,
21
yearGroup: data[element].yearGroup,
22
currentSchool: data[element].currentSchool,
23
parentOrGuardian: data[element].parentOrGuardian,
24
phoneNumber: data[element].phoneNumber,
25
homeAddress: data[element].homeAddress
26
};
27
}
28
29
return studentData;
30
31
}).map((filteredElement) => data[filteredElement])
32
33
})
34
35
}
36
37
38
return (
39
40
<div className="btn-textfield">
41
42
<button className="btn-textfield" onClick={infoGetter}>
43
Id: {id}
44
<br/><br/>
45
StudentInfo:
46
<br/><br/>
47
{studentData.studentName}
48
{studentData.yearGroup}
49
{studentData.currentSchool}
50
{studentData.parentOrGuardian}
51
{studentData.phoneNumber}
52
{studentData.homeAddress}
53
</button>
54
55
</div>
56
57
);
58
59
}
60
61
export default StudentInfo;
62
I think the problem is assigning the value to the object or sorting through. I appreciate any responses.
Advertisement
Answer
Sorry. It was a dumb mistake. I did response => response.json
forgetting the parenthsis.