0

I'm using webdriverIO, node. When I execute the following code in file1, Nothing gets printed to console. Although, I do see that the Post does add the data to the server. I noticed, that if I remove the module.exports and do a

try{returnAddPost("1","production","1.0")} 

and call the file directly with node file.js this works. But as soon as I add the module.exports the id is not displayed. I'm trying to get the ID of the item I added. That's all I want

In file1.js I have

 function addPost(clonedCycleId,env,version) {
     return new Promise((resolve, reject) => {
       var options = { url: url,body: jsonDataObjt,json: true
            };
    request.post(options, function (error, response) {
                console.dir("id of response"+response.body["id"]);
                if(error){reject(error)}
                resolve(response.body["id"]);
            })
        })
}

function returnAddPost(id,env,version) {
    return new Promise((resolve, reject) => {
        addPost(id,env,version)
            .then( function(response) {
                console.log("id of cycle that was added:"+response);
                resolve(response);
//here I will continue to add additional code that requires the id that I just added
            })
       .catch((error) => reject(error));
  } )
}

module.exports = {returnAddPost:returnAddPost};

In wdioConf.js file I have

var cycle = require('./file1'); 

after: function (result, capabilities, specs) {
       cycle.returnAddPost(id,env,version)
            .then(function(response){
               console.log("get ID "+response); //this is not displayed in console
            }).catch((error) => console.log(error));
},
Mary
  • 1
  • 1
  • 1
    Avoid the [Promise construction anti-pattern](https://stackoverflow.com/questions/23803743/what-is-the-explicit-promise-construction-antipattern-and-how-do-i-avoid-it) - specifically in `returnAddPost` – Jaromanda X Jan 16 '18 at 22:21
  • I gather the `;` at the end of `cycle.returnAddPost(id,env,version);` is a typo in the question, not your **actual** code (which would cause syntax errors) – Jaromanda X Jan 16 '18 at 22:24
  • Thanks for the info, I'm knew to promises and I didn't understand that I could directly return the response in a then statement. I deleted all the Promises in the code. instead of resolve(response). I added a then function and returned response In my config file what at the end worked was to add a "return" to cycle.returnAddPost() so that I could chain the promises – Mary Jan 17 '18 at 01:51

1 Answers1

0

I changed file1 to

var request = require('request-promise-native');

function addPost(clonedCycleId,env,version) {
   var options = { url: url,body: jsonDataObjt,json: true };
        return request.post(options);
    })
}

Then in the config file I changed the code to

after: function (result, capabilities, specs) {
        return cycle.addPost(id,env,version)
        .then(function(res){
            return res["id"];
        })
        .then(function(id){
            console.log(id)
        })
},
Mary
  • 1
  • 1