添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I currently have a Nestjs server setup and am attempting to perform an Axios request when one of the endpoints is hit with a GET request. Here is the controller.ts code:

@Controller()
export class TestController {
    constructor(private readonly testService: TestService) {}
    @Get('testData')
    testData() {
        return this.testService.testData();

Service.ts:

@Injectable()
export class TestService {
    status(): string {
        return 'OK'
    testData(): Promise<any> {
        return helper.getTestData();

Where helper.getTestData() is just a call to a helper file with the following function:

export async function getTestData(): Promise<any> {
    const result = await axios({
        url: tempURL,
        method: 'GET',
        timeout: 3000,
        httpsAgent: new https.Agent({
            rejectUnauthorized: false,

I am able to hit this endpoint tempURL but encounter the following error message: Cannot read property 'Agent' of undefined. I know that the endpoint I am attempting to hit requires a cert, which is why I must include the httpsAgent argument inside the Axios request. If I don't include the httpsAgent argument, I receive the following message Error: unable to verify the first certificate in nodejs.

Is there a way to configure Nestjs to work with https? Or is there another way to handle this authorization issue inside of Nestjs? Using Postman everything works fine so I'm assuming it is a Nestjs issue. Any help is appreciated.

@MicaelLevi https is defined, I have it properly imported into the file. Also, https is a native module, how could it be undefined? – Andrew Jul 3, 2021 at 23:29 depending on how you're importing it, it could. The line in the stack trace that the error Cannot read property 'Agent' of undefined appears is the same of the httpsAgent: new https.Agent one? – Micael Levi Jul 3, 2021 at 23:32 @MicaelLevi Yes, the error is on the httpsAgent: new https.Agent line. I import currently as follows: import https from 'https'; at the top of the file. – Andrew Jul 3, 2021 at 23:42 @MicaelLevi you were correct, the issue was with how I was importing. import https from 'https' did not work but import { Agent } from 'https' did. Thank you! – Andrew Jul 3, 2021 at 23:52

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.