Firestore where statement with array-containts doesn't work
up vote
0
down vote
favorite
I check my database for a reference (which exists) and i don't get any chats back but I don't see where.
chat.service.ts:
getUserChats(): Observable<Chat> {
let uid = '-1';
this.auth.currUser.subscribe(user => (uid = user.uid));
this.chats = this.afs
.collection('chats', ref => ref.where('members', 'array-contains', `users/${uid}`))
.snapshotChanges()
.pipe(
map(actions => {
return actions.map(action => {
const data = action.payload.doc.data() as Chat;
const id = action.payload.doc.id;
return {id, ...data};
});
})
) as Observable<Chat>;
return this.chats;
}
The code is intented to get all the chats where the logged in user is a member in. I do this by checking the members array, if there is any reference to him.
chat.component.ts:
chats: Observable<Chat>;
constructor(private router: Router, public chatService: ChatService, public dialog: MatDialog, public auth: AuthService) { }
ngOnInit() {
this.chats = this.chatService.getUserChats();
}
chat.component.html:
<mat-list-option *ngFor="let chat of chats | async" (click)="handleClick(chatList)" [value]="chat.id">
<img matListAvatar src="./assets/avatar.svg" alt="Avatar Icon">
<h3 mat-line>{{chat.name}}</h3>
<p mat-line>test</p>
</mat-list-option>
Chat structure:
javascript typescript firebase google-cloud-firestore
add a comment |
up vote
0
down vote
favorite
I check my database for a reference (which exists) and i don't get any chats back but I don't see where.
chat.service.ts:
getUserChats(): Observable<Chat> {
let uid = '-1';
this.auth.currUser.subscribe(user => (uid = user.uid));
this.chats = this.afs
.collection('chats', ref => ref.where('members', 'array-contains', `users/${uid}`))
.snapshotChanges()
.pipe(
map(actions => {
return actions.map(action => {
const data = action.payload.doc.data() as Chat;
const id = action.payload.doc.id;
return {id, ...data};
});
})
) as Observable<Chat>;
return this.chats;
}
The code is intented to get all the chats where the logged in user is a member in. I do this by checking the members array, if there is any reference to him.
chat.component.ts:
chats: Observable<Chat>;
constructor(private router: Router, public chatService: ChatService, public dialog: MatDialog, public auth: AuthService) { }
ngOnInit() {
this.chats = this.chatService.getUserChats();
}
chat.component.html:
<mat-list-option *ngFor="let chat of chats | async" (click)="handleClick(chatList)" [value]="chat.id">
<img matListAvatar src="./assets/avatar.svg" alt="Avatar Icon">
<h3 mat-line>{{chat.name}}</h3>
<p mat-line>test</p>
</mat-list-option>
Chat structure:
javascript typescript firebase google-cloud-firestore
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I check my database for a reference (which exists) and i don't get any chats back but I don't see where.
chat.service.ts:
getUserChats(): Observable<Chat> {
let uid = '-1';
this.auth.currUser.subscribe(user => (uid = user.uid));
this.chats = this.afs
.collection('chats', ref => ref.where('members', 'array-contains', `users/${uid}`))
.snapshotChanges()
.pipe(
map(actions => {
return actions.map(action => {
const data = action.payload.doc.data() as Chat;
const id = action.payload.doc.id;
return {id, ...data};
});
})
) as Observable<Chat>;
return this.chats;
}
The code is intented to get all the chats where the logged in user is a member in. I do this by checking the members array, if there is any reference to him.
chat.component.ts:
chats: Observable<Chat>;
constructor(private router: Router, public chatService: ChatService, public dialog: MatDialog, public auth: AuthService) { }
ngOnInit() {
this.chats = this.chatService.getUserChats();
}
chat.component.html:
<mat-list-option *ngFor="let chat of chats | async" (click)="handleClick(chatList)" [value]="chat.id">
<img matListAvatar src="./assets/avatar.svg" alt="Avatar Icon">
<h3 mat-line>{{chat.name}}</h3>
<p mat-line>test</p>
</mat-list-option>
Chat structure:
javascript typescript firebase google-cloud-firestore
I check my database for a reference (which exists) and i don't get any chats back but I don't see where.
chat.service.ts:
getUserChats(): Observable<Chat> {
let uid = '-1';
this.auth.currUser.subscribe(user => (uid = user.uid));
this.chats = this.afs
.collection('chats', ref => ref.where('members', 'array-contains', `users/${uid}`))
.snapshotChanges()
.pipe(
map(actions => {
return actions.map(action => {
const data = action.payload.doc.data() as Chat;
const id = action.payload.doc.id;
return {id, ...data};
});
})
) as Observable<Chat>;
return this.chats;
}
The code is intented to get all the chats where the logged in user is a member in. I do this by checking the members array, if there is any reference to him.
chat.component.ts:
chats: Observable<Chat>;
constructor(private router: Router, public chatService: ChatService, public dialog: MatDialog, public auth: AuthService) { }
ngOnInit() {
this.chats = this.chatService.getUserChats();
}
chat.component.html:
<mat-list-option *ngFor="let chat of chats | async" (click)="handleClick(chatList)" [value]="chat.id">
<img matListAvatar src="./assets/avatar.svg" alt="Avatar Icon">
<h3 mat-line>{{chat.name}}</h3>
<p mat-line>test</p>
</mat-list-option>
Chat structure:
javascript typescript firebase google-cloud-firestore
javascript typescript firebase google-cloud-firestore
asked Nov 7 at 10:22
filip
140110
140110
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
up vote
1
down vote
Apparently, you are querying for users/uid
while it is /users/uid
that is stored in your database, see the first /
Didn't work too, but good eyes!
– filip
Nov 7 at 13:14
1
FYI, I've just made some tests and slashes (/) in the array value do not create any problem (I suspected that maybe the / were the problem cause... but no). If I test your database model and values with a "pure" javascript code the query works well.
– Renaud Tarnec
Nov 7 at 13:23
add a comment |
up vote
0
down vote
accepted
The value '-1' will be searched for instantly because I'm not waiting for the User Observable to finish, that's why it searchs for -1 and finds nothing, so I have to wait for the user Observable. Still don't know how to return the inner observable then.
add a comment |
up vote
0
down vote
i think that you return "this.chat" from the service before the Observable comeback from the server .
you need to return the Observable himself....
try this change:
getUserChats(): Observable < Chat > {
let uid = '-1';
this.auth.currUser.subscribe(user => (
uid = user.uid;
//that what you need to return
return this.afs
.collection('chats', ref => ref.where('members', 'array-
contains ', `users/${uid}`))
.snapshotChanges()
.pipe(
map(actions => {
return actions.map(action => {
const data = action.payload.doc.data() as Chat;
const id = action.payload.doc.id;
return { id,...data };
});
})
) as Observable < Chat > ;
}
));
each Observable need subscribe - but in angular the async pipe make it for you - so you just need to pass him Observable.
i edit my answer - now the query waits to the answer from the user auth
Tried it and didn't work
– filip
Nov 7 at 12:47
try with the new code
– yehonatan yehezkel
Nov 7 at 15:52
I don't want to subscribe, I want to get the Observable as a return value @yehonatan yehezkel
– filip
Nov 7 at 15:59
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
Apparently, you are querying for users/uid
while it is /users/uid
that is stored in your database, see the first /
Didn't work too, but good eyes!
– filip
Nov 7 at 13:14
1
FYI, I've just made some tests and slashes (/) in the array value do not create any problem (I suspected that maybe the / were the problem cause... but no). If I test your database model and values with a "pure" javascript code the query works well.
– Renaud Tarnec
Nov 7 at 13:23
add a comment |
up vote
1
down vote
Apparently, you are querying for users/uid
while it is /users/uid
that is stored in your database, see the first /
Didn't work too, but good eyes!
– filip
Nov 7 at 13:14
1
FYI, I've just made some tests and slashes (/) in the array value do not create any problem (I suspected that maybe the / were the problem cause... but no). If I test your database model and values with a "pure" javascript code the query works well.
– Renaud Tarnec
Nov 7 at 13:23
add a comment |
up vote
1
down vote
up vote
1
down vote
Apparently, you are querying for users/uid
while it is /users/uid
that is stored in your database, see the first /
Apparently, you are querying for users/uid
while it is /users/uid
that is stored in your database, see the first /
answered Nov 7 at 10:47
Renaud Tarnec
8,80121431
8,80121431
Didn't work too, but good eyes!
– filip
Nov 7 at 13:14
1
FYI, I've just made some tests and slashes (/) in the array value do not create any problem (I suspected that maybe the / were the problem cause... but no). If I test your database model and values with a "pure" javascript code the query works well.
– Renaud Tarnec
Nov 7 at 13:23
add a comment |
Didn't work too, but good eyes!
– filip
Nov 7 at 13:14
1
FYI, I've just made some tests and slashes (/) in the array value do not create any problem (I suspected that maybe the / were the problem cause... but no). If I test your database model and values with a "pure" javascript code the query works well.
– Renaud Tarnec
Nov 7 at 13:23
Didn't work too, but good eyes!
– filip
Nov 7 at 13:14
Didn't work too, but good eyes!
– filip
Nov 7 at 13:14
1
1
FYI, I've just made some tests and slashes (/) in the array value do not create any problem (I suspected that maybe the / were the problem cause... but no). If I test your database model and values with a "pure" javascript code the query works well.
– Renaud Tarnec
Nov 7 at 13:23
FYI, I've just made some tests and slashes (/) in the array value do not create any problem (I suspected that maybe the / were the problem cause... but no). If I test your database model and values with a "pure" javascript code the query works well.
– Renaud Tarnec
Nov 7 at 13:23
add a comment |
up vote
0
down vote
accepted
The value '-1' will be searched for instantly because I'm not waiting for the User Observable to finish, that's why it searchs for -1 and finds nothing, so I have to wait for the user Observable. Still don't know how to return the inner observable then.
add a comment |
up vote
0
down vote
accepted
The value '-1' will be searched for instantly because I'm not waiting for the User Observable to finish, that's why it searchs for -1 and finds nothing, so I have to wait for the user Observable. Still don't know how to return the inner observable then.
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
The value '-1' will be searched for instantly because I'm not waiting for the User Observable to finish, that's why it searchs for -1 and finds nothing, so I have to wait for the user Observable. Still don't know how to return the inner observable then.
The value '-1' will be searched for instantly because I'm not waiting for the User Observable to finish, that's why it searchs for -1 and finds nothing, so I have to wait for the user Observable. Still don't know how to return the inner observable then.
answered Nov 7 at 13:10
filip
140110
140110
add a comment |
add a comment |
up vote
0
down vote
i think that you return "this.chat" from the service before the Observable comeback from the server .
you need to return the Observable himself....
try this change:
getUserChats(): Observable < Chat > {
let uid = '-1';
this.auth.currUser.subscribe(user => (
uid = user.uid;
//that what you need to return
return this.afs
.collection('chats', ref => ref.where('members', 'array-
contains ', `users/${uid}`))
.snapshotChanges()
.pipe(
map(actions => {
return actions.map(action => {
const data = action.payload.doc.data() as Chat;
const id = action.payload.doc.id;
return { id,...data };
});
})
) as Observable < Chat > ;
}
));
each Observable need subscribe - but in angular the async pipe make it for you - so you just need to pass him Observable.
i edit my answer - now the query waits to the answer from the user auth
Tried it and didn't work
– filip
Nov 7 at 12:47
try with the new code
– yehonatan yehezkel
Nov 7 at 15:52
I don't want to subscribe, I want to get the Observable as a return value @yehonatan yehezkel
– filip
Nov 7 at 15:59
add a comment |
up vote
0
down vote
i think that you return "this.chat" from the service before the Observable comeback from the server .
you need to return the Observable himself....
try this change:
getUserChats(): Observable < Chat > {
let uid = '-1';
this.auth.currUser.subscribe(user => (
uid = user.uid;
//that what you need to return
return this.afs
.collection('chats', ref => ref.where('members', 'array-
contains ', `users/${uid}`))
.snapshotChanges()
.pipe(
map(actions => {
return actions.map(action => {
const data = action.payload.doc.data() as Chat;
const id = action.payload.doc.id;
return { id,...data };
});
})
) as Observable < Chat > ;
}
));
each Observable need subscribe - but in angular the async pipe make it for you - so you just need to pass him Observable.
i edit my answer - now the query waits to the answer from the user auth
Tried it and didn't work
– filip
Nov 7 at 12:47
try with the new code
– yehonatan yehezkel
Nov 7 at 15:52
I don't want to subscribe, I want to get the Observable as a return value @yehonatan yehezkel
– filip
Nov 7 at 15:59
add a comment |
up vote
0
down vote
up vote
0
down vote
i think that you return "this.chat" from the service before the Observable comeback from the server .
you need to return the Observable himself....
try this change:
getUserChats(): Observable < Chat > {
let uid = '-1';
this.auth.currUser.subscribe(user => (
uid = user.uid;
//that what you need to return
return this.afs
.collection('chats', ref => ref.where('members', 'array-
contains ', `users/${uid}`))
.snapshotChanges()
.pipe(
map(actions => {
return actions.map(action => {
const data = action.payload.doc.data() as Chat;
const id = action.payload.doc.id;
return { id,...data };
});
})
) as Observable < Chat > ;
}
));
each Observable need subscribe - but in angular the async pipe make it for you - so you just need to pass him Observable.
i edit my answer - now the query waits to the answer from the user auth
i think that you return "this.chat" from the service before the Observable comeback from the server .
you need to return the Observable himself....
try this change:
getUserChats(): Observable < Chat > {
let uid = '-1';
this.auth.currUser.subscribe(user => (
uid = user.uid;
//that what you need to return
return this.afs
.collection('chats', ref => ref.where('members', 'array-
contains ', `users/${uid}`))
.snapshotChanges()
.pipe(
map(actions => {
return actions.map(action => {
const data = action.payload.doc.data() as Chat;
const id = action.payload.doc.id;
return { id,...data };
});
})
) as Observable < Chat > ;
}
));
each Observable need subscribe - but in angular the async pipe make it for you - so you just need to pass him Observable.
i edit my answer - now the query waits to the answer from the user auth
edited Nov 7 at 16:00
answered Nov 7 at 10:50
yehonatan yehezkel
36339
36339
Tried it and didn't work
– filip
Nov 7 at 12:47
try with the new code
– yehonatan yehezkel
Nov 7 at 15:52
I don't want to subscribe, I want to get the Observable as a return value @yehonatan yehezkel
– filip
Nov 7 at 15:59
add a comment |
Tried it and didn't work
– filip
Nov 7 at 12:47
try with the new code
– yehonatan yehezkel
Nov 7 at 15:52
I don't want to subscribe, I want to get the Observable as a return value @yehonatan yehezkel
– filip
Nov 7 at 15:59
Tried it and didn't work
– filip
Nov 7 at 12:47
Tried it and didn't work
– filip
Nov 7 at 12:47
try with the new code
– yehonatan yehezkel
Nov 7 at 15:52
try with the new code
– yehonatan yehezkel
Nov 7 at 15:52
I don't want to subscribe, I want to get the Observable as a return value @yehonatan yehezkel
– filip
Nov 7 at 15:59
I don't want to subscribe, I want to get the Observable as a return value @yehonatan yehezkel
– filip
Nov 7 at 15:59
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53187513%2ffirestore-where-statement-with-array-containts-doesnt-work%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown