我对 NodeJs 非常陌生,并且正在尝试运行测试。我正在尝试从 get 调用返回 http 响应,以便我可以对其运行断言。SendGetResponse() 返回promise 并且sendResponse 等待这个promise 被实现。但我得到 (node:2152) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'then' of undefined 并且 sendResponse 不等待。
任何帮助,将不胜感激!
async sendResponse(){
this.setUpRequest();
if (this.verb == 'get'){
var Promise1 = await this.sendGetRequest();
console.log("Going to resolve promise");
console.log(Promise1);
}
console.log('Leaving Sendresponse');
return this.ResponseMessage;
}
sendGetRequest() {
console.log("In SendGetRequest()");
const SendRequest = this.GetRequest(this.path,this.headers);
SendRequest.then(function(res,err){
return new Promise(function(resolve, reject){
if (err == null){
console.log("Resolving promise");
resolve(res.text);
}
else{
console.log("Rejecting promise");
reject(err);
}
}).catch(error => {
console.log('Exception caught', err.message);
});
});
}
GetRequest(path,headers){
console.log("In GetRequest()");
return chai.request(baseurl).get(path).set(headers);
}