Protractor: Getting undefined while trying to retu

2019-09-10 08:29发布

问题:

I have written a function which sends a GET request and returns the response.

this.generateToken = function() {
    var options = {
       uri: 'http://localhost:10000/token',
       method: 'GET',
       headers: {
         'Authorization': "YWRtaW46YWRtaW4="
       },
    };
    request(options, function (error, response, body) {
       var messageresponse = response.body.toString();
       console.log(messageresponse); //I am able to print the response
       return messageresponse;
    });
};

I am able to print the value of 'messageresponse' variable inside request().
This function is being called from one of my test:

it('Post a GET request and generate a response', function () {
    var response = commonFunctionObj.generateToken();
    response.then(function(value){     //Getting below mentioned error on this line
       console.log(value);
    });
});

Getting error: TypeError: Cannot read property 'then' of undefined in teh calling function.
Can someone please help?

回答1:

You need to create a promise and resolve it once to receive the response. Look at the below code.

this.generateToken = function() {
var deffered = protractor.promise.defer(); //create a promise
var options = {
   uri: 'http://localhost:10000/token',
   method: 'GET',
   headers: {
     'Authorization': "YWRtaW46YWRtaW4="
   },
};
  request(options, function (error, response, body) {
     var messageresponse = response.body.toString();
     console.log(messageresponse); 
     deffered.fulfill(messageresponse); //Instead of returning the response message, fulfill the promise that we created early.
  });

return deffered.promise; //return the created promise.
};

Now you can call the generateToken() method inside any of your test that will return a promise which is resolved only when the response is recieved from API call.

it('Post a GET request and generate a response', function () {
     var response = commonFunctionObj.generateToken(); 
     response.then(function(value){   
     console.log(value);
    });
});


回答2:

I think you can do it as follows;

this.generateToken = function() {
  var deferred = protractor.promise.defer();

  var options = {
   uri: 'http://localhost:10000/token',
   method: 'GET',
   headers: {
     'Authorization': "YWRtaW46YWRtaW4="
   },
  };
  request(options, function (error, response, body) {
   var messageresponse = response.body.toString();

   deferred.fulfill(messageresponse);
  });

  return deferred.promise;
};


it('Post a GET request and generate a response', function () {
    var response = commonFunctionObj.generateToken();
    response.then(function(value){     //Getting below mentioned error on this line
       console.log(value);
    });
});

Explanation;

You can't use .then with generateToken's return. Because there is no return inside of that function. You need to make a promise (protractor.promise) for using then inside of that. Then, you can use .then with generateToken function.



回答3:

You need to write print response logic in callback function, which is to resolve the promises or handling Asynchronous behavior

 Code  Snippet:

   it('Post a GET request and generate a response', function () {
      var response = commonFunctionObj.generateToken(function(err,res){ 
                            res.then(function(value){
                                 console.log(value); //or return  value
                         });
                });
    });