NgxPermissions hasPermission check inside an Observable method in Angular 6
up vote
1
down vote
favorite
I am using ngx-permissions to handle the permissions in my Angular 6 application. I would like to check the permissions of the user before retrieving data from an endpoint. Ngx-permissions provides a way to check the user's permission in the method hasPermission(permissionName). This returns a promise. I would like to retrieve the data from the endpoint using an Observable as I have read this is the Angular way to do things. However I'm not sure how to combine the Promise from the permission check and the Observable method.
Service:
getData(): Observable<Item<>>{
this.permissionsService.hasPermission(Permission.CanViewData)
.then(hasPermission => {
if (hasPermission) {
return this.http.get<Item>('http://endpoint/getdata', httpOptions).pipe(
map(this.extractData), // this is calculated too late
catchError(e => this.handleError(e, 'GetData', new Observable<Item>()))
);
}
});
return new Observable<Item>(); // this is always passed to the component
}
Component:
getData(): void {
this.service.getData().subscribe(data => {
this.data = data
});
}
I realise I'm not calling the hasPermission method correctly as my code always falls through to the final return new Observable<Item>();
. However the data is being retrieved from my endpoint - if I add a console.log I can see the result of map(this.extractData)
. It just is calculated too late. The component has already moved on and is using the empty Item
.
How can I use the permissionsService.hasPermission from ngx-permissions to check the permission of the user before attempting to retrieve the data and still return an Observable to my component?
angular observable
add a comment |
up vote
1
down vote
favorite
I am using ngx-permissions to handle the permissions in my Angular 6 application. I would like to check the permissions of the user before retrieving data from an endpoint. Ngx-permissions provides a way to check the user's permission in the method hasPermission(permissionName). This returns a promise. I would like to retrieve the data from the endpoint using an Observable as I have read this is the Angular way to do things. However I'm not sure how to combine the Promise from the permission check and the Observable method.
Service:
getData(): Observable<Item<>>{
this.permissionsService.hasPermission(Permission.CanViewData)
.then(hasPermission => {
if (hasPermission) {
return this.http.get<Item>('http://endpoint/getdata', httpOptions).pipe(
map(this.extractData), // this is calculated too late
catchError(e => this.handleError(e, 'GetData', new Observable<Item>()))
);
}
});
return new Observable<Item>(); // this is always passed to the component
}
Component:
getData(): void {
this.service.getData().subscribe(data => {
this.data = data
});
}
I realise I'm not calling the hasPermission method correctly as my code always falls through to the final return new Observable<Item>();
. However the data is being retrieved from my endpoint - if I add a console.log I can see the result of map(this.extractData)
. It just is calculated too late. The component has already moved on and is using the empty Item
.
How can I use the permissionsService.hasPermission from ngx-permissions to check the permission of the user before attempting to retrieve the data and still return an Observable to my component?
angular observable
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I am using ngx-permissions to handle the permissions in my Angular 6 application. I would like to check the permissions of the user before retrieving data from an endpoint. Ngx-permissions provides a way to check the user's permission in the method hasPermission(permissionName). This returns a promise. I would like to retrieve the data from the endpoint using an Observable as I have read this is the Angular way to do things. However I'm not sure how to combine the Promise from the permission check and the Observable method.
Service:
getData(): Observable<Item<>>{
this.permissionsService.hasPermission(Permission.CanViewData)
.then(hasPermission => {
if (hasPermission) {
return this.http.get<Item>('http://endpoint/getdata', httpOptions).pipe(
map(this.extractData), // this is calculated too late
catchError(e => this.handleError(e, 'GetData', new Observable<Item>()))
);
}
});
return new Observable<Item>(); // this is always passed to the component
}
Component:
getData(): void {
this.service.getData().subscribe(data => {
this.data = data
});
}
I realise I'm not calling the hasPermission method correctly as my code always falls through to the final return new Observable<Item>();
. However the data is being retrieved from my endpoint - if I add a console.log I can see the result of map(this.extractData)
. It just is calculated too late. The component has already moved on and is using the empty Item
.
How can I use the permissionsService.hasPermission from ngx-permissions to check the permission of the user before attempting to retrieve the data and still return an Observable to my component?
angular observable
I am using ngx-permissions to handle the permissions in my Angular 6 application. I would like to check the permissions of the user before retrieving data from an endpoint. Ngx-permissions provides a way to check the user's permission in the method hasPermission(permissionName). This returns a promise. I would like to retrieve the data from the endpoint using an Observable as I have read this is the Angular way to do things. However I'm not sure how to combine the Promise from the permission check and the Observable method.
Service:
getData(): Observable<Item<>>{
this.permissionsService.hasPermission(Permission.CanViewData)
.then(hasPermission => {
if (hasPermission) {
return this.http.get<Item>('http://endpoint/getdata', httpOptions).pipe(
map(this.extractData), // this is calculated too late
catchError(e => this.handleError(e, 'GetData', new Observable<Item>()))
);
}
});
return new Observable<Item>(); // this is always passed to the component
}
Component:
getData(): void {
this.service.getData().subscribe(data => {
this.data = data
});
}
I realise I'm not calling the hasPermission method correctly as my code always falls through to the final return new Observable<Item>();
. However the data is being retrieved from my endpoint - if I add a console.log I can see the result of map(this.extractData)
. It just is calculated too late. The component has already moved on and is using the empty Item
.
How can I use the permissionsService.hasPermission from ngx-permissions to check the permission of the user before attempting to retrieve the data and still return an Observable to my component?
angular observable
angular observable
edited Nov 20 at 15:11
asked Nov 20 at 14:50
Fletch
146
146
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
Yes, and for the record it is more general rxjs question. Anyway you have to convert the promise to observable and then chain the invocation like this:
import { of, from, Observable } from 'rxjs';
import { map, filter, flatMap } from 'rxjs/operators';
class Caller {
public constructor(){}
public call() : Observable<Item> {
//the promise returned from your method it has to be typed with boolean
var permission = new Promise<boolean>(function(resolve, reject) {
setTimeout(function() {
resolve(true);
}, 300);
});
//calling from(prommisse) converts promes to observable.
return from(permission)
.pipe(
filter(t => t), //just a trick to avoid if statemt in the flatMap call
flatMap( t => {
//call your http get method here
var it:Item = {property: "1"};
return of<Item>([it])
}))
}
}
export class Item {
property: String
}
That's brilliant, thanks Piotr
– Fletch
Nov 21 at 10:56
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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%2f53395616%2fngxpermissions-haspermission-check-inside-an-observable-method-in-angular-6%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
Yes, and for the record it is more general rxjs question. Anyway you have to convert the promise to observable and then chain the invocation like this:
import { of, from, Observable } from 'rxjs';
import { map, filter, flatMap } from 'rxjs/operators';
class Caller {
public constructor(){}
public call() : Observable<Item> {
//the promise returned from your method it has to be typed with boolean
var permission = new Promise<boolean>(function(resolve, reject) {
setTimeout(function() {
resolve(true);
}, 300);
});
//calling from(prommisse) converts promes to observable.
return from(permission)
.pipe(
filter(t => t), //just a trick to avoid if statemt in the flatMap call
flatMap( t => {
//call your http get method here
var it:Item = {property: "1"};
return of<Item>([it])
}))
}
}
export class Item {
property: String
}
That's brilliant, thanks Piotr
– Fletch
Nov 21 at 10:56
add a comment |
up vote
0
down vote
accepted
Yes, and for the record it is more general rxjs question. Anyway you have to convert the promise to observable and then chain the invocation like this:
import { of, from, Observable } from 'rxjs';
import { map, filter, flatMap } from 'rxjs/operators';
class Caller {
public constructor(){}
public call() : Observable<Item> {
//the promise returned from your method it has to be typed with boolean
var permission = new Promise<boolean>(function(resolve, reject) {
setTimeout(function() {
resolve(true);
}, 300);
});
//calling from(prommisse) converts promes to observable.
return from(permission)
.pipe(
filter(t => t), //just a trick to avoid if statemt in the flatMap call
flatMap( t => {
//call your http get method here
var it:Item = {property: "1"};
return of<Item>([it])
}))
}
}
export class Item {
property: String
}
That's brilliant, thanks Piotr
– Fletch
Nov 21 at 10:56
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
Yes, and for the record it is more general rxjs question. Anyway you have to convert the promise to observable and then chain the invocation like this:
import { of, from, Observable } from 'rxjs';
import { map, filter, flatMap } from 'rxjs/operators';
class Caller {
public constructor(){}
public call() : Observable<Item> {
//the promise returned from your method it has to be typed with boolean
var permission = new Promise<boolean>(function(resolve, reject) {
setTimeout(function() {
resolve(true);
}, 300);
});
//calling from(prommisse) converts promes to observable.
return from(permission)
.pipe(
filter(t => t), //just a trick to avoid if statemt in the flatMap call
flatMap( t => {
//call your http get method here
var it:Item = {property: "1"};
return of<Item>([it])
}))
}
}
export class Item {
property: String
}
Yes, and for the record it is more general rxjs question. Anyway you have to convert the promise to observable and then chain the invocation like this:
import { of, from, Observable } from 'rxjs';
import { map, filter, flatMap } from 'rxjs/operators';
class Caller {
public constructor(){}
public call() : Observable<Item> {
//the promise returned from your method it has to be typed with boolean
var permission = new Promise<boolean>(function(resolve, reject) {
setTimeout(function() {
resolve(true);
}, 300);
});
//calling from(prommisse) converts promes to observable.
return from(permission)
.pipe(
filter(t => t), //just a trick to avoid if statemt in the flatMap call
flatMap( t => {
//call your http get method here
var it:Item = {property: "1"};
return of<Item>([it])
}))
}
}
export class Item {
property: String
}
edited Nov 20 at 15:38
answered Nov 20 at 15:25
piotr szybicki
423210
423210
That's brilliant, thanks Piotr
– Fletch
Nov 21 at 10:56
add a comment |
That's brilliant, thanks Piotr
– Fletch
Nov 21 at 10:56
That's brilliant, thanks Piotr
– Fletch
Nov 21 at 10:56
That's brilliant, thanks Piotr
– Fletch
Nov 21 at 10:56
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2f53395616%2fngxpermissions-haspermission-check-inside-an-observable-method-in-angular-6%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