How to fix error Uncaught TypeError: Can not read property 'V' of undefined [duplicated]

0

I am bringing a list in JSON format, I store it in a variable and it works fine, the problem is when I try to use it is variable in another side it appears Uncaught TypeError: Can not read property 'V' of undefined, the V is brought from the list.

This is the line where the error is generated.

elementa = video.video[i].V;

The most curious thing is that it is inside a for and even inside if, the thing is when I increase the comparison variable of the if to more than 20 it throws me that error

for(var i = 0; i <= long.length; i++){
        if (i <= 30) {
            elementa = video.video[i].V;
            itemsa.push('<img id="' + elementa.toLowerCase().replace('.mp4', '') + '" src="http://localhost:700/proyecto-daniel/assets/web/img/if_male3_403019.png" alt="" >');
        }
    }

As we can see, the variable is at 30 and throws the error, if I try to use it on another side it generates the same error. someone knows how I can solve this

When doing a JSON log this is the result, as you can see if it is V

    
asked by Jhonny Luis 28.06.2018 в 06:54
source

3 answers

1

I think your problem is that you try to access a position in the array that does not exist. To solve that you have two options:

Option 1: Change the if

if (i <= video.video.length)

Option 2: change the for:

for(var i = 0; i <= video.video.length; i++)
    
answered by 28.06.2018 в 11:01
0

You have seen that the V property exists in all JSON positions.

You could modify the IF in the following way:

If(video.video[i].hasOwnProperty('V'))
    
answered by 28.06.2018 в 14:00
0

Remember that the array starts at position 0, therefore there is no array position [array.length], I would advise you to modify your for using the "<" and not the "<=" since you are accessing an invalid array position.

    
answered by 28.06.2018 в 15:11