1

I am trying to query a schedule collection where a field in the document = the users id, but i am getting this error: "Function Query.where() requires a valid third argument, but it was undefined."

     userId;
     uid;
     sched: Observable<ScheduleModel[]>;

The ngOnInit()

  ngOnInit() {
    this.userId = this.afAuth.authState.subscribe(user => {
      this.uid = user.uid;
    });
    this.getSched();
  }
getSched() {
  this.sched = this.afs.collectionGroup<ScheduleModel>('schedule', ref => ref.where('uid', '==', this.uid)).valueChanges();
}

this.uid in the query is giving me the trouble but:

  1. When i put the actual user uid 'iq6DkETjgMSMIdxw9cvLEzbf9Fr2' rather than this.uid, it works.
  2. In my template file when i put use {{ uid }} and it prints the correct id, so it is not actually undefined.

2 Answers 2

1

Try this

ngOnInit() {
let self = this;
this.userId = this.afAuth.authState.subscribe(user => {
  self.uid = user.uid;
});
this.getSched();
}

this in your this.uid is not in the same scope as you were using {{uid}}

Sign up to request clarification or add additional context in comments.

4 Comments

try the same thing with getSched() below, declare something like let self = this and use self.uid
Thanks for the reply. Still did not work, but you are correct about the scope. I created an alert box with the uid value and it gets displayed as 'Undefined' but then it still shows up in the view. I tried moving it to constructor with the same result.
@balance85 well, atleast we know what is the problem now
Ah.. I figured it out. Thanks for the help.
0
 this.userId = this.afAuth.authState.subscribe(user => {
    this.uid = user.uid;
    **this.sched = this.afs.collectionGroup<ScheduleModel>('schedule', ref => ref.where('uid', '==', this.uid)).valueChanges();
  });

moving the assignment of **this.sched seemed to do the trick.

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.