TypeError: Cannot read property 'length' of undefined in QML array

Aquarius_Girl

Alias.qml

import QtQuick 2.0

Rectangle {
    width: 100
    height: 62

    property var arr : [1,2,3,4]
}

Access.qml

import QtQuick 2.0

Rectangle {
    id: newq

    width: 100
    height: 62

    property var yy : [1]
    property var pp : [1]

    onPpChanged:
    {
        console.log("\npp: " +  pp.pop())
        console.log("\nyy: " + newq.yy.length + "\n")
    }
}

main.qml

import QtQuick 2.0

Rectangle {
    id: root
    width: 360
    height: 360

    Alias
    {
        id: poi
    }

    Access
    {
        pp: poi.arr
    }
}

The error shows up on this line:
console.log("\nyy: " + newq.yy.length + "\n")

Where am I going wrong?

Mitch

This happens because the yy property has not yet been initialised when pp is changed. You can see this by changing Access.qml:

import QtQuick 2.0

Rectangle {
    id: newq

    width: 100
    height: 62

    property var yy : [1]
    property var pp : [1]

    onYyChanged: console.log("yy changed: " + yy)

    onPpChanged:
    {
        console.log("pp changed: " + pp)
        console.log("\nyy: " + newq.yy.length + "\n")
    }
}

This outputs:

qml: pp changed: 1,2,3,4

file:///E:/Dev/Projects/qt/qml-test/Access.qml:17: TypeError: Cannot read property 'length' of undefined

qml: yy changed: 1

You can see that yy is eventually initialised, but not before pp. You should guard against this with an if (newq.yy) check, or refactor the code to avoid this situation, if possible.

The order of property assignments and bindings should not be relied upon, as QML is a declarative language.

Some related reading:

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

TypeError: Cannot read property 'length' of undefined array shuffle in react

From Dev

Uncaught TypeError: Cannot read property 'length' of undefined

From Dev

Uncaught TypeError: Cannot read property 'length' of undefined(…)

From Dev

opencv - TypeError: Cannot read property 'length' of undefined

From Dev

Nested JSON Array throwing "Uncaught TypeError: Cannot read property 'length' of undefined" when trying to iterate through

From Dev

Uncaught TypeError: Cannot read property 'length' of undefined when using setInterval

From Dev

Uncaught TypeError: Cannot read property 'length' of undefined JQUERY autocomplete

From Dev

DataTables - Uncaught TypeError: Cannot read property 'length' of undefined

From Dev

d3 TypeError: Cannot read property 'length' of undefined

From Dev

Getting Uncaught TypeError: Cannot read property 'length' of undefined on leafletjs

From Dev

.ajax JSON TypeError: Cannot read property 'length' of undefined

From Dev

clipboardData - Uncaught TypeError: Cannot read property 'length' of undefined

From Dev

Uncaught TypeError: Cannot read property 'length' of undefined when there is a parent

From Dev

Javascript: Simple Function - Uncaught TypeError: Cannot read property 'length' of undefined

From Dev

"Uncaught TypeError: Cannot read property 'length' of undefined" after defining var

From Dev

jquery error uncaught typeerror: cannot read property 'length' of undefined

From Dev

Ajax Uncaught TypeError: Cannot read property 'length' of undefined

From Dev

TypeError: Cannot read property 'length' of undefined in Angular app

From Dev

cannot read property'length' of undefined

From Dev

Undefined Array in constructor with TypeError: Cannot read property '0' of undefined

From Dev

Cannot read property 'length' of undefined sending array from node to jade

From Dev

Cannot read property 'length' of undefined sending array from node to jade

From Java

TypeError: Cannot read property 'then' of undefined

From Dev

TypeError: Cannot read property 'then' of undefined

From Dev

"TypeError: Cannot read property 'then' of undefined

From Dev

JavaScript Array - Uncaught TypeError: Cannot read property 'length'

From Dev

JavaScript Array - Uncaught TypeError: Cannot read property 'length'

From Dev

Angular pushing into an array gives: TypeError: Cannot read property 'push' of undefined

From Dev

QML: Cannot read property 'xxx' of undefined

Related Related

  1. 1

    TypeError: Cannot read property 'length' of undefined array shuffle in react

  2. 2

    Uncaught TypeError: Cannot read property 'length' of undefined

  3. 3

    Uncaught TypeError: Cannot read property 'length' of undefined(…)

  4. 4

    opencv - TypeError: Cannot read property 'length' of undefined

  5. 5

    Nested JSON Array throwing "Uncaught TypeError: Cannot read property 'length' of undefined" when trying to iterate through

  6. 6

    Uncaught TypeError: Cannot read property 'length' of undefined when using setInterval

  7. 7

    Uncaught TypeError: Cannot read property 'length' of undefined JQUERY autocomplete

  8. 8

    DataTables - Uncaught TypeError: Cannot read property 'length' of undefined

  9. 9

    d3 TypeError: Cannot read property 'length' of undefined

  10. 10

    Getting Uncaught TypeError: Cannot read property 'length' of undefined on leafletjs

  11. 11

    .ajax JSON TypeError: Cannot read property 'length' of undefined

  12. 12

    clipboardData - Uncaught TypeError: Cannot read property 'length' of undefined

  13. 13

    Uncaught TypeError: Cannot read property 'length' of undefined when there is a parent

  14. 14

    Javascript: Simple Function - Uncaught TypeError: Cannot read property 'length' of undefined

  15. 15

    "Uncaught TypeError: Cannot read property 'length' of undefined" after defining var

  16. 16

    jquery error uncaught typeerror: cannot read property 'length' of undefined

  17. 17

    Ajax Uncaught TypeError: Cannot read property 'length' of undefined

  18. 18

    TypeError: Cannot read property 'length' of undefined in Angular app

  19. 19

    cannot read property'length' of undefined

  20. 20

    Undefined Array in constructor with TypeError: Cannot read property '0' of undefined

  21. 21

    Cannot read property 'length' of undefined sending array from node to jade

  22. 22

    Cannot read property 'length' of undefined sending array from node to jade

  23. 23

    TypeError: Cannot read property 'then' of undefined

  24. 24

    TypeError: Cannot read property 'then' of undefined

  25. 25

    "TypeError: Cannot read property 'then' of undefined

  26. 26

    JavaScript Array - Uncaught TypeError: Cannot read property 'length'

  27. 27

    JavaScript Array - Uncaught TypeError: Cannot read property 'length'

  28. 28

    Angular pushing into an array gives: TypeError: Cannot read property 'push' of undefined

  29. 29

    QML: Cannot read property 'xxx' of undefined

HotTag

Archive