首页 > 解决方案 > Nodejs test giving error when part of the test is modularized into a different function

问题描述

I have a Nodejs test which has below part

const response1 = await request({
    method: HttpMethods.POST,
    path: `/v1/incidents/${emptyIncidentId}/assessments`,
    body: requestBody,
    auth: authentication
  })

And its giving the correct response. Now, I am trying to put the above into a separate async function because this gets repeated in many tests. So my function looks like below:

const response = doRequest(requestBody, emptyIncidentId, authentication)

And the function definition is :

async function doRequest (requestBody, incidentId, authentication) {
  return request({
    method: HttpMethods.POST,
    path: `/v1/incidents/${incidentId}/assessments`,
    body: requestBody,
    auth: authentication
  })

But this doRequest() call always responds with an empty response body. Could anyone tell me what I am doing wrong here. I checked the passed parameters, they are being passed correctly.

标签: node.js

解决方案


当您调用该函数时,请尝试使用await关键字。因为request是异步的

const response = await doRequest(requestBody, emptyIncidentId, authentication);

推荐阅读