node.jstreemodelskype-bots

treemodel + skype-bots Cannot read property 'model' of undefined


I'm working on skype bot app. I have issue with read the node from tree-model. Tree-model example follow the children node but my node has diff tab to read by system.

XML example-

<?xml version="1.0" encoding="UTF-8" ?>
<rootmenu id="1" title="menu" description="What scenario would you like to run?">
    <MenuOption id="1.1" title="Company Data" response="You entered company data" description="What action would you like to perform">
        <HierarchyMenuItem id="1.1.1.1" title="Select new data " response="You entered select new filter">
            <action>Filter</action>
        </HierarchyMenuItem>
        <HierarchyMenuItem id="1.1.1.2" title="Navigate node" response="You entered select node">
            <action description="Current Filter is ">Hierarchy</action>
            <HierarchyLevel level="1" name="One" navigateHierarchy="true">
                <action>RootAction</action>
                <Option title="Select a Country">
                    <OptionChoices id="1" title="One1" refNode="ForOne1" />
                    <OptionChoices id="2" title="One2" refNode="ForOne2" />
                    <OptionChoices id="3" title="One3" refNode="ForOne3" />
                    <OptionChoices id="4" title="One4" refNode="ForOne4" />
                </Option>
            </HierarchyLevel>
    </HierarchyMenuItem>
    </MenuOption>
    <MenuOption id="1.2" title="Adhoc Data">
        <Option>
            <OptionChoices id="1" title="Ad1" refNode="ForAdOne1" />
            <OptionChoices id="2" title="Ad2" refNode="ForAdOne2" />
            <OptionChoices id="3" title="Ad3" refNode="ForAdOne3" />
            <OptionChoices id="4" title="Ad4" refNode="ForAdOne4" />
        </Option>
    </MenuOption>
    <MenuOption id="1.3" title="(quit)">
    </MenuOption>
</rootmenu>

Read the xml from server-

function getXMLData(callback) { 
    var request = require('request');
    var DOMParser = require('xmldom').DOMParser;
    var simpleconvertxml = require('simpleconvert-xml');
    request('http://demo.in/RefactoredXML.xml', function (error, response, body) {
        if (!error && response.statusCode == 200) {
            var xmlnode = new DOMParser().parseFromString([body].join('\n'), 'text/xml');
            var myNumber = simpleconvertxml.getXMLAsObj(xmlnode);
            treeRoot = tree.parse(myNumber.rootmenu);
            callback(treeRoot);
        }
    })
}

My first bot call

bot.dialog('/menu', [
    function (session, args) {
        getXMLData(function (treeRoot) { 
            //session.send('node place:'+treeRoot.model.title);
            var firstChild = [];
            for (var i = 0; i < treeRoot.model.MenuOption.length; i++) {
                if(treeRoot.model.MenuOption[i].title !='' && treeRoot.model.MenuOption[i].title != undefined && treeRoot.model.MenuOption[i].title != null) { 
                    firstChild.push(treeRoot.model.MenuOption[i].title);
                }
            }
            if(firstChild.length > 0) { 
                builder.Prompts.choice(session, treeRoot.model.description,firstChild );
// it shows builder.Prompts.choice(session, "What scenario would you like to run? ", "company data|adhoc data|(quit)");
            } else {
                session.send('Something went wrong. You can use the back or top command.');
            }
        });
    },
    function (session, results) {
        if (results.response && results.response.entity != '(quit)') {
            session.userData.profile.treeSelectdNodeTitle   = results.response.entity;
            getXMLData(function (treeRoot) { 
                for (var i = 0; i < treeRoot.model.MenuOption.length; i++) {
                    if(treeRoot.model.MenuOption[i].title == session.userData.profile.treeSelectdNodeTitle) { 
                        session.userData.profile.treeSelectdNodeId      = treeRoot.model.MenuOption[i].id;
                        session.userData.profile.treeSelectdResponse    = treeRoot.model.MenuOption[i].response;
                    }
                }
                session.send('resp ' + session.userData.profile.treeSelectdResponse);
        if(treeRoot.hasChildren()) {  
                session.send('in the children');
                session.beginDialog('/get Tree Node');
            } else { 
                session.send('in the title');
                session.beginDialog('/'+ treeRoot.model.title);
            }
            });

        } else {
            // Exit the menu
            session.endDialog();
        }
    },
    function (session, results) {
        // The menu runs a loop until the user chooses to (quit).
        session.replaceDialog('/menu');
    }
]).reloadAction('reloadMenu', null, {matches: /^menu|show menu|top|top menu/i});

Menu dialog call should display 1.company data 2. adhoc data 3. (quit) but its showing only 1.company data 2.adhoc data and Issue in the menu is if user select the 1 option as Company Data it will not goes to treeRoot.hasChildren() condition.

bot.dialog('/get Tree Node', [
    function(session,agrs) { 
        getTreeNode(session);
    }
]);


function getTreeNode(session) { 
    session.send("in tree "+ session.userData.profile.treeSelectdNodeId);
    getXMLData(function(treeRoot) {
        var nextLevel = treeRoot.first(function (node) {
            return node.model.id === session.userData.profile.treeSelectdNodeId;
        });
        session.send('selected title '+ nextLevel.model.HierarchyMenuItem[0].title);
        /* var secondListChild = [];
        for(var i = 0; i < nextLevel.model.HierarchyMenuItem.length; i++) {
            if(nextLevel.model.HierarchyMenuItem[i].title !='' && nextLevel.model.HierarchyMenuItem[i].title != undefined && nextLevel.model.HierarchyMenuItem[i].title != null) { 
                secondListChild.push(nextLevel.model.HierarchyMenuItem[i].title);
            }
        }
        if(secondListChild.length > 0) { 
            builder.Prompts.choice(session, nextLevel.model.description,secondListChild); 
        } else { 
            session.send('Something went wrong. You can use the back or top command.');
        } */
    }); 
}

issue with the getTreeNode is session.send('selected title '+ nextLevel.model.HierarchyMenuItem[0].title); ^ TypeError: Cannot read property 'model' of undefined


Solution

  • Got Solution....

    I have change the XML parse NPM and its working for me..

    var parse       = require('xml-parser');
    var inspect     = require('util').inspect;
    function getXMLData(callback) { 
        var request = require('request');
        request('http://ztdemo.headfitted.in/RefactoredXML.xml', function (error, response, body) {
            if (!error && response.statusCode == 200) {
                var obj = parse(body);
                //console.log(inspect(obj, { colors: true, depth: Infinity }));
                //callback(treeRoot);
    
                treeRoot = tree.parse(obj.root);
                callback(treeRoot);
            }
        })
    }