0

I have code in one of my file like below:

this.xmlObjectRepositoryLoader = function (xmlPath){
        var innerMap = {};
        var elementName;
        var filePath = xmlPath+'.xml'
        var self = this
        return new Promise(
            function(resolve, reject){
                console.log("In xmlObjectRepositoryLoader : "+filePath)
                self.readFilePromisified(filePath)
                .then(text => {
                    var doc = domparser.parseFromString(text,"text/xml");
                    var elements = doc.getElementsByTagName("Element");
                    for(var i =0 ; i< elements.length;i++){
                        var elm = elements[i];
                        elementName = elm.getAttribute("name");
                        var params = elm.getElementsByTagName("param");
                        innerMap = {};
                        for(var j =0 ; j< params.length;j++){
                            var param = params[j];
                            var locatorType = param.getAttribute("type");
                            var locatorValue = param.getAttribute("value");
                            innerMap[locatorType] = locatorValue;
                        }
                        map[elementName] = innerMap;
                        innerMap={};
                    }
                    console.log(map) // prints the map
                    resolve(text)
                })
                .catch(error => {
                    reject(error)
                });
            });
        }

this.readFilePromisified = function(filename) {
        console.log("In readFilePromisified : "+filename)
        return new Promise(
            function (resolve, reject) {
                fs.readFile(filename, { encoding: 'utf8' },
                (error, data) => {
                    if (error) {
                        reject(error);
                    } else {
                        resolve(data);
                    }
                })
            })
        }

I am calling above function from another file as below:

objectRepositoryLoader.readObjectRepository(fileName)
    .then(text => {
        console.log(text);
    })
    .catch(error => {
        console.log(error);
    });

But it gives me error as

 .then(text => {   ^

TypeError: Cannot read property 'then' of undefined

In this case how can I use promise to call another promise function and then use the returned value in one more promise function and return calculated value to calling function where I can use the value in other functions. I sound a bit confused. Please help

Referrence link: Node.js : Call function using value from callback or async

Following is readObjectRepository definition :

readObjectRepository = function(fileName) {
        var filePath = '../'+fileName;
        xmlObjectRepositoryLoader(filePath)        
    }
Community
  • 1
  • 1
Abhinav
  • 1,037
  • 6
  • 20
  • 43
  • What is the definition of the method - readObjectRepository? – AdityaReddy Sep 13 '16 at 09:15
  • It returns a HashMap. I have to parse the hashmap and extract value. – Abhinav Sep 13 '16 at 09:26
  • Can you provide the method Definition of readObjectRepository. That will help understand your error – AdityaReddy Sep 13 '16 at 11:14
  • I have edited the pots. I tried returning promise from here but it also did not worked – Abhinav Sep 13 '16 at 11:23
  • I have provided the answer. Pls let me know if that works for you – AdityaReddy Sep 13 '16 at 11:36
  • Avoid the [`Promise` constructor antipattern](http://stackoverflow.com/q/23803743/1048572) (in `xmlObjectRepositoryLoader`)! – Bergi Sep 13 '16 at 11:41
  • Please don't [edit](http://stackoverflow.com/revisions/39465791/4) your post to an entirely different question. See [Why is my variable unaltered after I modify it inside of a function?](http://stackoverflow.com/q/23667086/1048572) and [Is it bad practice to have a constructor function return a Promise?](http://stackoverflow.com/q/24398699/1048572) for your new problem. If you can solve it with those references, please [ask a new question](http://stackoverflow.com/questions/ask) – Bergi Sep 13 '16 at 12:09

1 Answers1

3

I got the issue in your code. The method readObjectRepository doesn't return a promise and infact doesn't return anything

So you cannot chain .then

To do that- in the function definition of readObjectRepository return the promise of xmlObjectRepositoryLoader

Make this change and it should be all good

readObjectRepository = function(fileName) {
        var filePath = '../'+fileName;
        return xmlObjectRepositoryLoader(filePath)   //This will return the promise of xmlObjectRepositoryLoader which you can handle in 'then' and obtain the text
    }
AdityaReddy
  • 3,625
  • 12
  • 25