我寫了一個函數,它發送GET請求並返回響應。量角器:嘗試返回值時出現undefined
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;
});
};
我能夠在request()中打印'messageresponse'變量的值。
此功能正從我的測試的一個叫:
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);
});
});
四處錯誤:在格蘭調用函數TypeError: Cannot read property 'then' of undefined
。
有人可以幫忙嗎?
謝謝Sudharsan。 – Praveen