'subscribe' is deprecated. Use an observer instead of a complete callback

Viewed 6054

Hi am having the following error with SonarQube 'subscribe' is deprecated. Use an observer instead of a complete callback. Am using formly in angular 9. Thank you for your help and time

              onInit: (field: UiFormFieldConfig) => {
                const CostsControl = FormUtil.getControl(field.form, ContactDetailsFieldKey.Costs);
                CostsControl?.valueChanges.pipe(takeUntil(this.destroy$)).subscribe((Cost: string) => {
                  if (Cost) {
                    const costsToSet = !Codes ? null : Cost;
                    field?.formControl?.setValue(costsToSet);
                  }
                });
              },
            },

Please find updated code and yet sonarQube is popping this message 'subscribe' is deprecated. Use an observer instead of a complete callback

  hooks: {
              onInit: (field: UiFormFieldConfig) => {
                const CostsControl = FormUtil.getControl(field.form, ContactDetailsFieldKey.Costs);
                CostsControl ?.valueChanges.pipe(takeUntil(this.destroy$)).subscribe({
                  next: (Cost: string) => {
                    if (Cost) {
                      const costsToSet = !Codes ? null : Cost;
                      field?.formControl?.setValue(costsToSet);
                    }
                  },
                });
              },
            },
1 Answers

Based on the answer @Philipp Meissner mentioned, you should simple do:

CostsControl?.valueChanges.pipe(takeUntil(this.destroy$)).subscribe(
    next: (Cost: string) => {
        if (Cost) {
            const costsToSet = !Codes ? null : Cost;
            field?.formControl?.setValue(costsToSet);
        }
    },
    error: () => { // log error }
});
Related