0

I am trying to write an Angular app that loads a different login page based on the "groupId" that I set in the URL. The idea is to send each client a URL that contains a particular "groupId" as a parameter. A template is used to load the page where the page's text and pictures are pulled from the firebase repo using that specific company's groupId.

I tried using query params as follows:

(In my component):

  ngOnInit() {
    console.log('test');

    console.log(this.activeRoute.queryParams);
     this.activeRoute.queryParams
    .subscribe(params => {
      this.groupId = params.groupId;
   })
   console.log(this.groupId);
   if (this.groupId === undefined) {
    this.router.navigate(['/login/locked']);
   }
  }

(and my routes in the parent component):

const routes: Routes = [
  {
    path: 'login',
    component: LoginComponent,
    children: [
      {
        path: '', component: SignInComponent,
      },
      {
        path: 'signup', component: SignUpComponent,
      },
      {
        path: 'locked', component: LockScreenComponent,
      },
      {
        path: 'password-rest', component: PasswordResetComponent,
      }
    ]
  }
];

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule]
})
export class AuthPagesRoutingModule { }

However, I keep on getting an undefined from the console.log(this.groupId) when I paste www.example.com/login?groupId=FACEBOOK235 into the search bar (and thereby navigating me to the locked screen).

Am I missing something?

3 Answers 3

1

because your if condition is outside subscribe the queryParams observables is asyncronous. your code should be :-

ngOnInit() {
    console.log('test');

    console.log(this.activeRoute.queryParams);
     this.activeRoute.queryParams
    .subscribe(params => {
      this.groupId = params.groupId;
      console.log(this.groupId);
      if (this.groupId === undefined) {
       this.router.navigate(['/login/locked']);
      }
   })
  }

(EDIT) When I log the complete parameter list I get:

closed: false
destination: BehaviorSubject {_isScalar: false, observers: Array(0), closed: false, isStopped: false, hasError: false, …}
hasError: false
isStopped: false
observers: []
operator: MapOperator {thisArg: undefined, project: ƒ}
source: BehaviorSubject {_isScalar: false, observers: Array(0), closed: false, isStopped: false, hasError: false, …}
thrownError: null
_isScalar: false
[[Prototype]]: Subject
complete: ƒ complete()
constructor: class AnonymousSubject
error: ƒ error(err)
next: ƒ next(value)
_subscribe: ƒ _subscribe(subscriber)
[[Prototype]]: Observable
asObservable: ƒ asObservable()
complete: ƒ complete()
constructor: class Subject
error: ƒ error(err)
lift: ƒ lift(operator)
next: ƒ next(value)
unsubscribe: ƒ unsubscribe()
_subscribe: ƒ _subscribe(subscriber)
_trySubscribe: ƒ _trySubscribe(subscriber)
Symbol(rxSubscriber): ƒ [_internal_symbol_rxSubscriber__WEBPACK_IMPORTED_MODULE_2__.rxSubscriber]()
[[Prototype]]: Object
Sign up to request clarification or add additional context in comments.

9 Comments

Just a correction, this particular observable is asynchronous, but not all observables are asynchronous - some may be synchronous.
observables can never be syncronous, their callback will always be asyncronous, thats the whole reason we define a callback.
Disagree, please check out this simple code on codesandbox (make sure to open the console): codesandbox.io/s/synchronous-rxjs-qjfs0k?file=/src/index.ts But I agree that one should never relay on this behavior.
ok let me edit the answer. thanks for adding to my knowledge. i read more here :- christianlydemann.com/are-observables-async
@AakashGarg I tried your solution but it still sends back undefined... any other suggestions?
|
1

You subscribe an observable and they are asynchronous and your if condition outside the subscribe method so when you access your groupId propery you get undefined. you should access it in subscribe method.

ngOnInit() {
     this.activeRoute.queryParams
    .subscribe(params => {
      this.groupId = params.groupId;
      console.log(this.groupId);
      if (this.groupId === undefined) {
         this.router.navigate(['/login/locked']);
      }

   })   
  }

Comments

0

Call to this.activeRoute.queryParams.subscribe is not synchronous, therefore console.log keeps reporting undefined. Add this check inside subscribe, like so:

this.activeRoute.queryParams
 .subscribe(params => {
   this.groupId = params.groupId;
   console.log(this.groupId);
   if (this.groupId === undefined) {
     this.router.navigate(['/login/locked']);
   }
})

Also, you may like to check Guards - that's something you might need: https://angular.io/guide/router-tutorial-toh#milestone-5-route-guards . By adding CanActivate guard which returns true or URL tree, you'll achieve what you want.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.