Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

how to handle NULL from a data request using QML doc.responseXML.documentElement



  • Hello,

    I am trying to handle NULL from a data request using QML doc.responseXML.documentElement:

    if (doc.readyState === XMLHttpRequest.DONE) {
                    //Check is not NULL
                    if(!doc.responseXML.documentElement) {
                        var a = doc.responseXML.documentElement;
    
    
                    }
    }
    

    but I get that an error: TypeError: Value is null and could not be converted to an object
    So how do I catch null value coming from the doc.responseXML.documentElement ?

    thx


  • Moderators

    @biskero
    please show the whole code handling your XMLHttpRequest



  • This post is deleted!


  • This is the code, data is received correctly but after the check DONE, I want to make sure if not NULL but I get the error:
    TypeError: Value is null and could not be converted to an object

    on the var a = doc.responseXML.documentElement; code line

    if (doc.readyState === XMLHttpRequest.DONE) {
                    //Check is not NULL
                    if(!doc.responseXML.documentElement) {
                        var a = doc.responseXML.documentElement;
    for (var ii = 0; ii < a.childNodes.length; ++ii) {
                            //showRequestInfo(a.childNodes[ii].nodeName);
                            if(a.childNodes[ii].nodeName === "light") {
                                console.log("a="+a.childNodes[ii].childNodes[0].nodeValue);
                            }
    }
    }
    

  • Moderators

    @biskero
    also the code before that block



  • if (doc.readyState === XMLHttpRequest.LOADING) {
                    //Do something while loading
    
                } else if (doc.readyState === XMLHttpRequest.HEADERS_RECEIVED) {
                    //showRequestInfo("Headers -->");
                    //showRequestInfo(doc.getAllResponseHeaders ());
                    //showRequestInfo("Last modified -->");
                    //showRequestInfo(doc.getResponseHeader ("Last-Modified"));
    
                } else if (doc.readyState === XMLHttpRequest.DONE) {
                    //Check is not NULL
                    if(!doc.responseXML.documentElement) {
                        var a = doc.responseXML.documentElement;
    for (var ii = 0; ii < a.childNodes.length; ++ii) {
                            //showRequestInfo(a.childNodes[ii].nodeName);
                            if(a.childNodes[ii].nodeName === "light") {
                                console.log("a="+a.childNodes[ii].childNodes[0].nodeValue);
                            }
    }
    }
    

  • Moderators

    @biskero
    ok once more.
    I am not interested in any more detail implementations from the inside of your function, showing code beyond the point you want to do your check isnt relevant.
    I want to see the code beginning from where you create the XMLHttpRequest object.



  • the code come from this example

     MouseArea {
                id: mouseArea
                anchors.fill: parent
                onClicked: {
                    log.text = ""
                    console.log("\n")
    
                    var doc = new XMLHttpRequest();
                    doc.onreadystatechange = function() {
                        if (doc.readyState == XMLHttpRequest.HEADERS_RECEIVED) {
                            showRequestInfo("Headers -->");
                            showRequestInfo(doc.getAllResponseHeaders ());
                            showRequestInfo("Last modified -->");
                            showRequestInfo(doc.getResponseHeader ("Last-Modified"));
    
                        } else if (doc.readyState == XMLHttpRequest.DONE) {
                            var a = doc.responseXML.documentElement;
                            for (var ii = 0; ii < a.childNodes.length; ++ii) {
                                showRequestInfo(a.childNodes[ii].nodeName);
                            }
                            showRequestInfo("Headers -->");
                            showRequestInfo(doc.getAllResponseHeaders ());
                            showRequestInfo("Last modified -->");
                            showRequestInfo(doc.getResponseHeader ("Last-Modified"));
                        }
                    }
    
                    doc.open("GET", "data.xml");
                    doc.send();
                }
            }
    

    I just add the code to check the if object is not NULL

    if(!doc.responseXML.documentElement)
    

  • Moderators

    @biskero

    1. try console.log(doc.responseXML.documentElement) and console.log(typeof doc.responseXML.documentElement) and check the console output, it might give some help
    2. where does this file come from? its important what the Content-Type is of the response.
    3. try doc.responseXML.documentElement == null or doc.responseXML.documentElement == undefined

Log in to reply