0

Probably I know what causing this error, but I don't know how to fix it. I have some code with subscribe:

  .subscribe(([a, b]: [any, any]) => {
    if (this.files.length > 0) {
      const setImagesStatus = (b.body.objectData as DtoSetAppBackgroundImagesStatus);
      if (setImagesStatus.success) {
        this.hasBackgroundImages = true;
        this.files = [];
        notify('Data successfully updated', 'success');
      } else {
        notify('Application background images upload error. ' + setImagesStatus.info, 'error', 9000);
      }
    } else {
      notify('Data successfully updated', 'success');
    }
    const parsedUserBeforeChanges = new UserPreLogDataConverter(this.groupsService,this.groups).GetPreLogData(updateUserDto);
    const parsedUserAfterChanges = new UserPreLogDataConverter(this.groupsService,this.groups).GetPreLogData(this.userWithNoChanges);
    this.eventLogEndpointSvc
      .addLogs(this.serverEventLogger
        .LogChanges(JSON.parse(JSON.stringify(parsedUserBeforeChanges)),JSON.parse(JSON.stringify(parsedUserAfterChanges)),
    () => {
      this.userCustomLogFunction(parsedUserBeforeChanges,parsedUserAfterChanges);
    }
    )).subscribe().add(this.userWithNoChanges=JSON.parse(JSON.stringify(updateUserDto)));}
  );

My this.userCustomLogFunction looks like this:

userCustomLogFunction(userWithNoChanges: UserPreLogData,updateUserDto: UserPreLogData): Array<DtoEventLog>
{
  const oldGroupIds = userWithNoChanges.groups.map(x=>x.id);
  const newGroupIds = updateUserDto.groups.map(x=>x.id);

  const addedToGroups = newGroupIds.filter(x => !oldGroupIds.some(y => JSON.stringify(x) === JSON.stringify(y)));
  const deletedFromGroups = oldGroupIds.filter(x => !newGroupIds.some(y => JSON.stringify(x) === JSON.stringify(y)));

  const groupLogs:  DtoEventLog[] = [];
  for(const id in addedToGroups)
  {
      const evLog = new DtoEventLog();
      evLog.methodName = 'LiftaKitMobileAppTestUsers.Update';
      evLog.objectId=addedToGroups[id].toString();
      evLog.login=this.authService.getUser().login;
      evLog.firstName =this.authService.getUser().firstName;
      evLog.lastName = this.authService.getUser().lastName;
      evLog.email = this.authService.getUser().email;
      evLog.logType=EventLogType.UPDATE.toString();
      evLog.scope=EventLogScope.GROUP.toString();
      evLog.input ='Added user' + updateUserDto.firstName + ' ' + updateUserDto.lastName + ' login: ' + updateUserDto.login + ' no: '+ updateUserDto.no +', id: ' + updateUserDto.iD;
      groupLogs.push(evLog);
  }

  for(const id in deletedFromGroups)
  {
      const evLog = new DtoEventLog();
      evLog.methodName = 'LiftaKitMobileAppTestUsers.Update';
      evLog.objectId=deletedFromGroups[id].toString();
      evLog.login=this.authService.getUser().login;
      evLog.firstName =this.authService.getUser().firstName;
      evLog.lastName = this.authService.getUser().lastName;
      evLog.email = this.authService.getUser().email;
      evLog.logType=EventLogType.UPDATE.toString();
      evLog.scope=EventLogScope.GROUP.toString();
      evLog.input ='Deleted user' + updateUserDto.firstName + ' ' + updateUserDto.lastName + ' login: ' + updateUserDto.login + ' no: '+ updateUserDto.no +', id: ' + updateUserDto.iD;
      groupLogs.push(evLog);
  }

  return groupLogs;
}

So as you see nothing special, and it works fine but.. Inside method LogChanges I calling this method (passed as argument, and called as logMethod()) and it always returns undefined. I don't know how to fix it...

LogChanges<Type>(ob1: Type,ob2: Type=null,logMethod: Function = () => new Array<DtoEventLog>()): Array<DtoEventLog> {
const additionalRelation = logMethod();
//rest code here
 }

What should I do? I think there's something badly written in the first subscribe where I calling AddLogs and LogChanges.

The Trainer
  • 633
  • 1
  • 3
  • 19
  • What error? Which part is returning undefined? – evolutionxbox Jun 29 '22 at 10:25
  • this.userCustomLogFunction - it returns undefined – The Trainer Jun 29 '22 at 10:26
  • 1
    It is not returning `undefined`, you're just not doing anything with the value that is returned. Either remove the curly brackets of the arrow function, or add `return` in front of `this.userCustomLogFunction(...)`. – Ivar Jun 29 '22 at 10:31
  • Gotcha! You should add answer and I would accept it ;) Thank you @Ivar – The Trainer Jun 29 '22 at 11:03
  • 1
    The mistake of not using `return` in an arrow function with curly brackets is one that has been made many times before on Stack Overflow. No need for yet another answer. :-) – Ivar Jun 29 '22 at 11:10

0 Answers0