SHARE
TWEET

Untitled

a guest Feb 22nd, 2019 66 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. @Injectable()
  2. export class NewProviderService {
  3.     private serviceSubject: BehaviorSubject<HttpResponseModel[]>;
  4.     // this is the variable that should be exposed. make the subject as private
  5.     // this allows the service to be the sole propertier to modify the stream and
  6.     // not the controller or components
  7.     serviceSubject$:  Observable<HttpResponseModel[]>;
  8.  
  9.     private serviceErrorSubject: BehaviorSubject<any>;
  10.     serviceErrorSubject$: Observable<any>;
  11.     filePath: string;
  12.     httpResponseObjectArray: HttpResponseModel[];
  13.     constructor() {
  14.         this.serviceSubject = new BehaviorSubject<HttpResponseModel[]>([]);
  15.         this.serviceSubject$ = this.serviceSubject.asObservable();
  16.  
  17.         this.serviceErrorSubject = new BehaviorSubject<any>(null);
  18.         this.serviceErrorSubject$ = this.serviceErrorSubject.asObservable();
  19.  
  20.         this.filePath = path.resolve(__dirname, './../../shared/assets/httpTest.json');
  21.     }
  22.      readFileFromJson() {
  23.           return new Promise((resolve, reject) => {
  24.             fs.exists(this.filePath.toString(), exists => {
  25.                 if (exists) {
  26.                     fs.readFile(this.filePath.toString(), 'utf-8' , (err, data) => {
  27.                         if (err) {
  28.                             logger.info('error in reading file', err);
  29.                             return reject('Error in reading the file' + err.message);
  30.                         }
  31.  
  32.                         logger.info('file read without parsing fg', data.length);
  33.                         if ((data.length !== 0) && !isNullOrUndefined(data) && data !== null) {
  34.                             // this.httpResponseObjectArray = JSON.parse(data).HttpTestResponse;
  35.                             // logger.info('array obj is:', this.httpResponseObjectArray);
  36.                             logger.info('file read after parsing new', JSON.parse(data));
  37.                             return resolve(JSON.parse(data).HttpTestResponse);
  38.                         } else {
  39.                             return reject(new FileExceptionHandler('no data in file'));
  40.                         }
  41.                     });
  42.                 } else {
  43.                     return reject(new FileExceptionHandler('file cannot be read at the moment'));
  44.                 }
  45.             });
  46.           });
  47.     }
  48.  
  49.  
  50.         getData() {
  51.                  from(this.readFileFromJson()).pipe(map(data => {
  52.                         logger.info('data in obs', data);
  53.                         this.httpResponseObjectArray = data as HttpResponseModel[];
  54.                         return this.httpResponseObjectArray;
  55.                  }), catchError(error => {
  56.                      return Observable.throw(error);
  57.                  }))
  58.                  .subscribe(actualData => {
  59.                     this.serviceSubject.next(actualData);
  60.                 }, err => {
  61.                     logger.info('err in sub', typeof err, err);
  62.                     this.serviceErrorSubject.next(err);
  63.                 });
  64.             }
  65.      
  66. @Get('/getJsonData')
  67.   public async getJsonData(@Req() requestAnimationFrame,@Req() req, @Res() res) {
  68.  
  69.     await this.newService.getData();
  70.     this.newService.serviceSubject$.subscribe(data => {
  71.       logger.info('data subscribed', data, _.isEmpty(data));
  72.       if (!isNullOrUndefined(data) && !_.isEmpty(data)) {
  73.         logger.info('coming in');
  74.         res.status(HttpStatus.OK).send(data);
  75.         res.end();
  76.       }
  77.     });
  78.     }
  79.      
  80. Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
  81.     at ServerResponse.setHeader (_http_outgoing.js:470:11)
  82.     at ServerResponse.header (C:personalNodetest-nest.jsprj-samplenode_modulesexpresslibresponse.js:767:10)
  83.     at Ser
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top