'Unable to hit api define inside cron job in NestJs

I am creating Nest application where on GET request I have cron in which I want to perform some functionality and inside cron I am hitting third party api service but unable to get response. I am using reqres.in free api endpoint.

Below is my code:

user.controller.ts

import { Body, Controller, Get, Post } from '@nestjs/common';
import { UserService } from './user.service';

@Controller()
export class UserController {
constructor(private userService:UserService){}

@Get('user')
async getUsers(){
    const all = await this.userService.users();
    return all;
  }
}

user.service.ts

import { HttpService } from '@nestjs/axios';
import { Injectable, Logger } from '@nestjs/common';
import { SchedulerRegistry } from '@nestjs/schedule';
import { CronJob } from 'cron';
import { map } from 'rxjs';

@Injectable()
export class UserService {
constructor(private httpService:HttpService,
    private schedulerRegistry:SchedulerRegistry){}

    private readonly logger = new Logger(UserService.name);

async users(){

    const job = new CronJob('*/1 * * * *', async () => {
       
        this.logger.log('added');
        console.log("My cron running...");
      
        const url = 'https://reqres.in/api/unknown';
        this.logger.log('Inside api call');
        const me = await this.httpService.get(url).pipe(
           map((resp) => {   
            this.logger.log(resp.data);
            return resp.data;
           })
        );

        return me;
      });
    
      this.schedulerRegistry.addCronJob('sec',job);
      job.start(); 
      
      console.log('job added');
    }
}

Someone let me know how can I get response. Any help would be appreciated.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source