Hope someone here would be able to help me out.
I am trying to build an Owl-Carousel from an array of objects. but for some reason the array is not been recognized, would appreciate if someone could help to find the mistake I have made here.
The error messages I am getting is:
‘item’ is not defined. and ‘tv’ is assigned a value but never used.
Here you have the code:
JavaScript
x
42
42
1
const tv = [
2
{
3
name: 'Meu Pedacinho de Chao 01',
4
personagem: 'Barbeiro',
5
ano: 2014,
6
de: 'Benedito Ruy Barbosa',
7
img: { mpc1 },
8
alt: 'Meu Pedacinho de Chao 01'
9
10
}
11
12
]
13
14
export class Televisao extends Component {
15
render() {
16
return (
17
<div class='container-fluid' >
18
tv.forEach(function (item) {
19
<OwlCarousel items={3}
20
className="owl-theme"
21
loop
22
nav
23
margin={8} >
24
25
<div className='item card' >
26
<img className='"card-img-top" ' src={item.img} alt={item.alt} />
27
<div className='card-body' >
28
<h5 className="card-title" > ${item.name} </h5>
29
< p class="card-text" > ${item.personagem} </p>
30
< p class="card-text" > ${item.ano} </p>
31
< p class="card-text" > ${item.de} </p>
32
</div >
33
</div >
34
</OwlCarousel >
35
}
36
</div>
37
)
38
}
39
}
40
41
export default Televisao;
42
Advertisement
Answer
Any expression Javascript should be inside curly brackets “{}” and replace forEach by map cause forEach doesn’t return anything and I notice that you use a function and doesn’t return anything to fix this you can add return before OwlCarousel component or use arrow function with parentheses.
JavaScript
1
41
41
1
const tv = [
2
{
3
name: 'Meu Pedacinho de Chao 01',
4
personagem: 'Barbeiro',
5
ano: 2014,
6
de: 'Benedito Ruy Barbosa',
7
img: { mpc1 },
8
alt: 'Meu Pedacinho de Chao 01'
9
10
}
11
12
]
13
14
export class Televisao extends Component {
15
render() {
16
return (
17
<div className="container-fluid">
18
{tv.map((item)=>(
19
<OwlCarousel items={3}
20
className="owl-theme"
21
loop
22
nav
23
margin={8}>
24
<div className="item card" >
25
<img className="card-img-top" src={item.img} alt={item.alt} />
26
<div className="card-body">
27
<h5 className="card-title">{item.name}</h5>
28
<p className="card-text">{item.personagem}</p>
29
<p className="card-text">{item.ano}</p>
30
<p className="card-text">{item.de}</p>
31
</div>
32
</div>
33
</OwlCarousel>
34
)}
35
</div>
36
)
37
}
38
}
39
40
export default Televisao;
41