Hi all!
I am trying to send the registration key of a device from client side to my server. I manage to get the registration key into my local Storage but when I try to send it smtg bad happens. I would appreciate any help.
Thank you!
Hi all!
I am trying to send the registration key of a device from client side to my server. I manage to get the registration key into my local Storage but when I try to send it smtg bad happens. I would appreciate any help.
Thank you!
Hi there
Can you please copy/paste code as text here, not screen shots
Next, it will be interesting to console.log all the variables you assume having value. Especially the one that is null, where you expect a value - currentUserValue
Next, getting the value of an observable is only to be done using subscribe. Where there is an oddity at your end where you apparently use localstorage (sync function?) then convert it into async stuff and then get the data sync (this.currentToken.source.etc)
Yes of course. It seems like the variable “this.currentToken.source._value” has value and it is printed but after that when the registerToken service is called with it as parameter the parameter is null for some reason.
Here is the code:
test.ts
import { Component } from '@angular/core';
import { BehaviorSubject } from 'rxjs';
import { UserService } from 'src/app/services/user.service';
@Component({
selector: 'app-test',
templateUrl: 'test.page.html',
styleUrls: ['test.page.scss'],
})
export class TestPage {
registrationId: any;
currentToken: any;
constructor(private userService:UserService
) {}
ngOnInit(){
this.registrationId = new BehaviorSubject<String>(localStorage.getItem('fcm'));
this.currentToken = this.registrationId.asObservable();
console.log("AAAAAAA")
console.log(this.currentToken.source._value)
this.userService.registerToken(this.currentToken.source._value)
}
}
user.service.ts ( It’s not the whole file but I don’t think it has to do with the rest)
registerToken(token:any): Observable<any> {
const headers2 = new HttpHeaders({
Authorization: 'Bearer ' + this.currentUserValue.token,
"Content-Type": "application/json; charset=UTF-8"
});
console.log("THIS IS THE TOKEN: "+token);
return this.http.post(API_URL+"register-token", token, { headers: headers2 });
}
Please use proper code blocks by using three backticks before and after the code - Extended Syntax | Markdown Guide.
Hi @tmalachias,
It seems to me that this
doesn’t have the same context in ngOnInit
and registerToken
functions.
For this
to have the same context, you can try to use function call
as this.userService.registerToken.call(this, this.currentToken.source._value)
.
Or being simpler, pass the authorization token as a second parameter to registerToken
like registerToken(token:string, currentUserToken: string)
.
I think you simply have a typo in your registerToken
function. Instead of using this.currentUserValue.token
when assembling your Authorization header use the passed in token
parameter instead.