These are chat archives for angular/angular-2-ionic-2

16th
Mar 2018
Teodor Kolev
@Teodor_ASA_twitter
Mar 16 11:06
Hi guys, why my method did not work. It returns subscription, but I want to return string. How to make it work?
public translateValue(key: string): string {
    return this.translate.get(key).subscribe((res: string) => {
      return res;
    });
  }
it says type 'Subscription' is not assignable to type 'string'
Zurie
@zurie
Mar 16 11:08
because you subscribed to it and returned the subscription
if you want a string out of the subscription, the subscription contains a value which is the string you are looking for
look inside res and you will see it
Teodor Kolev
@Teodor_ASA_twitter
Mar 16 11:09
res is the value itself
I have also tried this but it don't work
Zurie
@zurie
Mar 16 11:12
take the string off and then see wht res gives you
Teodor Kolev
@Teodor_ASA_twitter
Mar 16 11:12
  public translateValue(key: string): string {
    let result: string;
    this.translate.get(key).subscribe((res: string) => {
      result = res;
    });
    return result;
  }
Zurie
@zurie
Mar 16 11:12
is this a pipe
Teodor Kolev
@Teodor_ASA_twitter
Mar 16 11:12
It shows that result is not defined
Zurie
@zurie
Mar 16 11:12
or is this a function
Teodor Kolev
@Teodor_ASA_twitter
Mar 16 11:13
it is a function
Zurie
@zurie
Mar 16 11:14
i did this subscribe
 updateUser(value): void {
    this.loading.present();
    this.fab.updateUser(this.navParams.get('myUser').CustomerID, value)
      .subscribe(
      res => {
        console.log(res);
        this.loadUser('email', this.fab.fab_user);
      },
      err => {
        // Log errors if any
        console.log(err);
      });
  }
Owen Melbourne
@OwenMelbz
Mar 16 11:15
thats not how it works
subscribe is like then - its an async function
you cant return a value from it
you'd have to do something like
this.translateValue('something').then(result => console.log(result));
or similar to @zurie
but yeah you cant return a value from a async function such as subscribe
you CAN assign in the callbacks (like zurie)
Zurie
@zurie
Mar 16 11:17
loadHistory(id) {

    this.fab.getHistory(id)
      .subscribe(
        res => {
          // debugger;

          this.profile.history = res ,
            this.loading.dismiss(),
            this.queried = true;
        },
        err => {
          // Log errors if any
          console.log(err);
        });
  }
<div class="invoice-wrapper"
         *ngFor="let invoice of this.profile.history |  orderBy : '-' | search: myProgress; let i=index"
         [ngClass]="{active: isLevel1Shown('idx'+i)}">
makes it really hard to understand out of context
but basically its not like json of the past where you just get data and return key values
its like now you are subscribing to a stream
and the stream has your data but also has all this other RXjS stuff
Teodor Kolev
@Teodor_ASA_twitter
Mar 16 11:20
@zurie Does this code for me or not?
Zurie
@zurie
Mar 16 11:21
no
nothing really codes for you
Owen Melbourne
@OwenMelbz
Mar 16 11:23
@Teodor_ASA_twitter maybe do some reading on how subscriptions and promises work and it will make it more clear
Teodor Kolev
@Teodor_ASA_twitter
Mar 16 11:24
@OwenMelbz maybe that apply to all people here. Lets close this chat?
Zurie
@zurie
Mar 16 11:28
if you want some good examples you can goto google and type this site:stackblitz.com angular observable
search the stackblitz.com site and whatever you want and you can find lots of examples from people sorta like jsfiddle or codepen that have angular observables and subscribes etc etc. then when you see a few its easier to get
Teodor Kolev
@Teodor_ASA_twitter
Mar 16 11:29
there is millions other sites with examples
please if you don't know what is the answer (as me) don't give answer from sort "google it"
Owen Melbourne
@OwenMelbz
Mar 16 11:30
the chat isnt to teach people how javascript principles work - the chat is for general q/a about ionic. if you need help with specific piece of code of functionality then websites like stackoverflow exist to get solutions to actual problems
Teodor Kolev
@Teodor_ASA_twitter
Mar 16 11:33
I have asked for a help. My question wasn't "Please give me link for subscription examples"
Owen Melbourne
@OwenMelbz
Mar 16 11:33
and the answer was
subscriptions cannot return values
Zurie
@zurie
Mar 16 11:33
also stackblitz.com lets you make an example and share it, so if you want maximum help, you can make a example with stackblitz, and then say the question, and then link it and then its easier to see it in context
Owen Melbourne
@OwenMelbz
Mar 16 11:33
they return promises
Zurie
@zurie
Mar 16 11:33
no they dont
promises are 1 time
Owen Melbourne
@OwenMelbz
Mar 16 11:34
sure, but in this example
Teodor Kolev
@Teodor_ASA_twitter
Mar 16 11:34
really? so why in component I can set a string to return value of subscription
Owen Melbourne
@OwenMelbz
Mar 16 11:34
point is if he returns a subscription - he's not gonna get the value from it
Zurie
@zurie
Mar 16 11:34
right
strings are not subscriptions
or integers
so you can't get "hello world" from it
or 2.25
Teodor Kolev
@Teodor_ASA_twitter
Mar 16 11:34
yes I can
private someStr: string;
  private translate(key: string): void {
    this.translate.get(key).subscribe((res: string) => {
      this.someStr = res;
    });
  }
this works in component
public translateValue(key: string): string {
    return this.translate.get(key).subscribe((res: string) => {
      return res;
    });
  }
this not works in provider
Owen Melbourne
@OwenMelbz
Mar 16 11:36
because you're returning the subscribe
not the value
the 1st one
your just running a function
then assigning a value to it
Teodor Kolev
@Teodor_ASA_twitter
Mar 16 11:37
this is what I try to do in 2nd
Owen Melbourne
@OwenMelbz
Mar 16 11:37
what happens if you change the code to like
let result: string = 'before subscribe';

this.translate.get(key).subscribe((res: string) => {
  result = 'from subscribe';
});

console.log(result);

return result;
what does the console log show?
Teodor Kolev
@Teodor_ASA_twitter
Mar 16 11:37
run a function and assign value for return
Owen Melbourne
@OwenMelbz
Mar 16 11:38
the "subscribe" function is async
so it wont execute in sequence
imagine if your subscriped called an API that took 20 seconds to respond
the code carries on pass the subscribe
20 seconds later the code is returned
but theres nothing there to handle it now as its already happened
your code basically does
let test = function () {
    return setTimeout(function () {
        return 'lemonade';
    }, 3000)
}

test();
the function returns a setTimeout
it does not return the value lemonade
the setTimeout DOES return lemonade
but because its async it does not chain together
in 3 seconds it just randomly returns lemonade
into a black hole
Teodor Kolev
@Teodor_ASA_twitter
Mar 16 11:43
@OwenMelbz console.log: from subscribe
Owen Melbourne
@OwenMelbz
Mar 16 11:46
bloody race conditions, so there is the change that IF the code happens fast enough it will work, but if its an api call to like twitter it will likely take longer
this.translate.get(key).subscribe((res: string) => {
  console.log('1234')
});

console.log('5678');
which comes first?
Teodor Kolev
@Teodor_ASA_twitter
Mar 16 11:49
I have no time for ur black holes
The thing I am trying to do is possible
don't tell me it is impossible
Owen Melbourne
@OwenMelbz
Mar 16 11:49
yes its possible, you're just doing it wrong lol thats the point
  • you need to really sort your attitude out 👌🏼
Teodor Kolev
@Teodor_ASA_twitter
Mar 16 11:50
my point is: get value from subscription and return it as a string
Owen Melbourne
@OwenMelbz
Mar 16 11:51
first google result
says you dont
deal with it
Teodor Kolev
@Teodor_ASA_twitter
Mar 16 11:51
"you need to really sort your attitude out " - the only thing that I can agree with you
can you show any line of that page that answers my question?
Owen Melbourne
@OwenMelbz
Mar 16 11:52
no because you're doing it wrong 😂 read the link it tells you its wrong, and how to do it right
UncertaintyP
@UncertaintyP
Mar 16 12:07
Hey @OwenMelbz do you test on iOS 9 as well?
Owen Melbourne
@OwenMelbz
Mar 16 12:08
I have not tbh, normally we agree support with our clients, typically we only ever support the latest version, unless they pay for backward compatibility
UncertaintyP
@UncertaintyP
Mar 16 12:08
I see
Owen Melbourne
@OwenMelbz
Mar 16 12:10
Easy way out 😄
UncertaintyP
@UncertaintyP
Mar 16 12:10
Believe me, it is :D
Owen Melbourne
@OwenMelbz
Mar 16 12:11
Not sure what version of iOS Apple gave access to nitro to other apps - guess before then ionic would be sluggish