3

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);
                    }
                  },
                });
              },
            },
snoopy
  • 105
  • 2
  • 10
  • maybe you need to add "next": func with depricaded method? .subscribe({ next: this.handleUpdateResponse.bind(this), error: this.handleError.bind(this) }); – Hubi Jan 15 '21 at 12:21
  • see: https://stackoverflow.com/questions/55472124/subscribe-is-deprecated-use-an-observer-instead-of-an-error-callback – novarx Jan 15 '21 at 12:25
  • 1
    Does this answer your question? [Subscribe is deprecated: Use an observer instead of an error callback](https://stackoverflow.com/questions/55472124/subscribe-is-deprecated-use-an-observer-instead-of-an-error-callback) – Philipp Meissner Jan 15 '21 at 12:38
  • @PhilippMeissner not really sounds a bit confusing for me. Any guide will be heartly appreciated :) – snoopy Jan 15 '21 at 12:46
  • @snoopy Does it sound confusing, or are the provided examples simply not working? If so, what exactly did you try? (Add the code + errors to your question). – Philipp Meissner Jan 15 '21 at 12:56
  • I am using Angular 10, typescript 4.0.7, [this answer used ](https://stackoverflow.com/a/66691738/5146390) – Maple13 Mar 25 '21 at 07:13

1 Answers1

0

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 }
});
iamdlm
  • 1,885
  • 1
  • 11
  • 21
  • 1
    i did same , but sonarQube sitll complain 'subscribe' is deprecated. Use an observer instead of a complete callback. Is there another approach to this issue – snoopy Jan 18 '21 at 18:00
  • Edit your initial question and add the code you've tried. If you did everything right the error has to go away. – iamdlm Jan 18 '21 at 18:14
  • again for your help . Please find my updated question with the new code shown below the my initial code . Thank you for your time and response – snoopy Jan 19 '21 at 11:42