1

I am getting an error in console while i am trying to get a specific value from json data. This is the error:

'Uncaught TypeError: Cannot read property 'processen_id' of undefined'

Here is my code:

$.get("/getProces", function (data) {
        if (data.error) {
        } else {
            for (var i = 0; i <= data.message.length; i++) {
                var obj = data.message[i]
                console.log(obj.processen_id)
            }
        }
    })
}

This is what i get when i log (data):

console.log(data)

1
  • Have you tried logging data and data.message to see what's inside? Obviously the message[i] doesn't exist Commented Jun 22, 2017 at 11:40

3 Answers 3

1

You have a mistake in your code in the for loop

<= instead of <

 $.get("/getProces", function (data) {
            if (data.error) {
            } else {
                for (var i = 0; i < data.message.length; i++) {
                    var obj = data.message[i]
                    console.log(obj.processen_id)
                }
            }
        })
    }
Sign up to request clarification or add additional context in comments.

Comments

1

The error message means that obj is undefined. That means, that data.message[i] gets an undefined value. The problem is the loop. You get an i that is larger then the array. Change <= to <:

for (var i = 0; i < data.message.length; i++) {
   var obj = data.message[i]
   console.log(obj.processen_id)
}

Comments

0

index out of range: for (var i = 0; i <= data.message.length; i++) { should be for (var i = 0; i < data.message.length; i++) { instead.

you can also optimize your code in this way:

$
  .get("/getProces")
  .then((res) => res.error ? Promise.reject(res) : Promise.resolve(res))
  .then((data) => {
    for (var i = 0; i < data.message.length; i++) {
      var obj = data.message[i]
      console.log(obj.processen_id)
    }
  })

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.