【发布时间】:2019-10-07 20:59:32
【问题描述】:
我正在为 Angular 应用程序设置访问控制,并像这样对我的用户对象进行建模: -用户 - 名 - 电话号码 --posts[]
getPosts(): Observable<any[]> {
return this.postsCollection.snapshotChanges().pipe(
map((actions) => {
return actions.map((a) => {
const data = a.payload.doc.data();
const data2 = { id: a.payload.doc.id, ...data};
this.hasPostAccess(data2);
return { id: a.payload.doc.id, ...data};
});
})
);
}
private hasPostAccess(data: any) {
const inspect = obj => {
for (const prop in obj) {
if (obj.hasOwnProperty(prop)) {
// console.log(`${prop}: ${obj[prop]}`);
if (prop === 'id') {
console.log(obj[prop]);
const PostId = obj[prop];
const currentUser = JSON.stringify(localStorage.getItem('user'));
if (currentUser.search(PostId) === -1) {
console.log('Denied: ' + PostId);
} else {
console.log('Allowed: ' + PostId);
}
console.log('Current USER: ' + JSON.stringify(currentUser));
// if (currentUser(PostId) > -1) {
// console.log('Allowed: ' + PostId);
// } else {
// console.log('Denied: ' + PostId);
// }
}
}
}
};
inspect(data);
}
我想显示属于该用户的所有帖子。因此,当我订阅 getPosts Observable 时,它必须过滤帖子并仅返回存储在用户帖子数组中的帖子 ID。
【问题讨论】:
-
问题出在哪里?你期待我们为你做你的工作吗? How to ask
-
不,迈克,只是想知道如何在不使用道具检查的情况下使用 Observable 完成此操作。我是 rxjs 的新手。 :)
-
谢谢@penleychan
标签: angular typescript rxjs google-cloud-firestore