angular NGRX: Not able to select single value in ngrx entity based on id
I am trying to select an object by Id from the entity state. Below is my reducer.
export interface MessageState extends EntityState<Message>
// additional entities state properties
loaded: boolean;
loading: boolean;
export const adapter: EntityAdapter<Message> = createEntityAdapter<Message>(
selectId: (msg: Message) => msg.messageId,
);
export const initialState: MessageState = adapter.getInitialState(
// additional entity state properties
loaded: false,
loading: false,
);
export function reducer(state = initialState, action: MessageActionsUnion): MessageState
switch (action.type)
case MessageActionTypes.UPSERT_Message:
return ...state, loading: true, loaded: false ;
case MessageActionTypes.UPSERT_Message_SUCCESS:
return adapter.upsertOne(action.payload.Message,
...state, loaded: true, loading: false,
);
default:
return state;
Below is my index.ts file
export interface State extends fromRoot.AppState
queueModule: QueueState;
export interface QueueState
msgHeaders: fromMsgHeaders.MessageHeaderState
export const reducers: ActionReducerMap<QueueState> =
msgHeaders: fromMsgHeaders.reducer
;
export const getQueueState$ = createFeatureSelector<QueueState>('queueModule');
I am trying to form a selector when passed an Id will return the value from the entity.
export const selectMessages = createSelector(
fromFeatures.getQueueState$,
(state: fromFeatures.QueueState) => state.msgs
);
export const
selectAll: selectAllMessages,
selectEntities: selectMessagesEntities,
selectIds: selectMessagesIds,
selectTotal: selectMessagesTotal
= adapter.getSelectors(selectMessages);
I looked at many resources, but none are clear on how to select an object by passing the id.
angular typescript dictionary ngrx ngrx-entity
add a comment |
I am trying to select an object by Id from the entity state. Below is my reducer.
export interface MessageState extends EntityState<Message>
// additional entities state properties
loaded: boolean;
loading: boolean;
export const adapter: EntityAdapter<Message> = createEntityAdapter<Message>(
selectId: (msg: Message) => msg.messageId,
);
export const initialState: MessageState = adapter.getInitialState(
// additional entity state properties
loaded: false,
loading: false,
);
export function reducer(state = initialState, action: MessageActionsUnion): MessageState
switch (action.type)
case MessageActionTypes.UPSERT_Message:
return ...state, loading: true, loaded: false ;
case MessageActionTypes.UPSERT_Message_SUCCESS:
return adapter.upsertOne(action.payload.Message,
...state, loaded: true, loading: false,
);
default:
return state;
Below is my index.ts file
export interface State extends fromRoot.AppState
queueModule: QueueState;
export interface QueueState
msgHeaders: fromMsgHeaders.MessageHeaderState
export const reducers: ActionReducerMap<QueueState> =
msgHeaders: fromMsgHeaders.reducer
;
export const getQueueState$ = createFeatureSelector<QueueState>('queueModule');
I am trying to form a selector when passed an Id will return the value from the entity.
export const selectMessages = createSelector(
fromFeatures.getQueueState$,
(state: fromFeatures.QueueState) => state.msgs
);
export const
selectAll: selectAllMessages,
selectEntities: selectMessagesEntities,
selectIds: selectMessagesIds,
selectTotal: selectMessagesTotal
= adapter.getSelectors(selectMessages);
I looked at many resources, but none are clear on how to select an object by passing the id.
angular typescript dictionary ngrx ngrx-entity
1
Maybe it's just me, but I can see nothing in here that tries to select any object by passing some Id somewhere... My guess is that you want to get this mysterious message by its id... but I can see no storage for those messages to begin with. It is completely unclear what you're trying to do.
– Alexander Leonov
Nov 8 '18 at 3:01
add a comment |
I am trying to select an object by Id from the entity state. Below is my reducer.
export interface MessageState extends EntityState<Message>
// additional entities state properties
loaded: boolean;
loading: boolean;
export const adapter: EntityAdapter<Message> = createEntityAdapter<Message>(
selectId: (msg: Message) => msg.messageId,
);
export const initialState: MessageState = adapter.getInitialState(
// additional entity state properties
loaded: false,
loading: false,
);
export function reducer(state = initialState, action: MessageActionsUnion): MessageState
switch (action.type)
case MessageActionTypes.UPSERT_Message:
return ...state, loading: true, loaded: false ;
case MessageActionTypes.UPSERT_Message_SUCCESS:
return adapter.upsertOne(action.payload.Message,
...state, loaded: true, loading: false,
);
default:
return state;
Below is my index.ts file
export interface State extends fromRoot.AppState
queueModule: QueueState;
export interface QueueState
msgHeaders: fromMsgHeaders.MessageHeaderState
export const reducers: ActionReducerMap<QueueState> =
msgHeaders: fromMsgHeaders.reducer
;
export const getQueueState$ = createFeatureSelector<QueueState>('queueModule');
I am trying to form a selector when passed an Id will return the value from the entity.
export const selectMessages = createSelector(
fromFeatures.getQueueState$,
(state: fromFeatures.QueueState) => state.msgs
);
export const
selectAll: selectAllMessages,
selectEntities: selectMessagesEntities,
selectIds: selectMessagesIds,
selectTotal: selectMessagesTotal
= adapter.getSelectors(selectMessages);
I looked at many resources, but none are clear on how to select an object by passing the id.
angular typescript dictionary ngrx ngrx-entity
I am trying to select an object by Id from the entity state. Below is my reducer.
export interface MessageState extends EntityState<Message>
// additional entities state properties
loaded: boolean;
loading: boolean;
export const adapter: EntityAdapter<Message> = createEntityAdapter<Message>(
selectId: (msg: Message) => msg.messageId,
);
export const initialState: MessageState = adapter.getInitialState(
// additional entity state properties
loaded: false,
loading: false,
);
export function reducer(state = initialState, action: MessageActionsUnion): MessageState
switch (action.type)
case MessageActionTypes.UPSERT_Message:
return ...state, loading: true, loaded: false ;
case MessageActionTypes.UPSERT_Message_SUCCESS:
return adapter.upsertOne(action.payload.Message,
...state, loaded: true, loading: false,
);
default:
return state;
Below is my index.ts file
export interface State extends fromRoot.AppState
queueModule: QueueState;
export interface QueueState
msgHeaders: fromMsgHeaders.MessageHeaderState
export const reducers: ActionReducerMap<QueueState> =
msgHeaders: fromMsgHeaders.reducer
;
export const getQueueState$ = createFeatureSelector<QueueState>('queueModule');
I am trying to form a selector when passed an Id will return the value from the entity.
export const selectMessages = createSelector(
fromFeatures.getQueueState$,
(state: fromFeatures.QueueState) => state.msgs
);
export const
selectAll: selectAllMessages,
selectEntities: selectMessagesEntities,
selectIds: selectMessagesIds,
selectTotal: selectMessagesTotal
= adapter.getSelectors(selectMessages);
I looked at many resources, but none are clear on how to select an object by passing the id.
angular typescript dictionary ngrx ngrx-entity
angular typescript dictionary ngrx ngrx-entity
asked Nov 7 '18 at 23:01
prabhat gundepalliprabhat gundepalli
144114
144114
1
Maybe it's just me, but I can see nothing in here that tries to select any object by passing some Id somewhere... My guess is that you want to get this mysterious message by its id... but I can see no storage for those messages to begin with. It is completely unclear what you're trying to do.
– Alexander Leonov
Nov 8 '18 at 3:01
add a comment |
1
Maybe it's just me, but I can see nothing in here that tries to select any object by passing some Id somewhere... My guess is that you want to get this mysterious message by its id... but I can see no storage for those messages to begin with. It is completely unclear what you're trying to do.
– Alexander Leonov
Nov 8 '18 at 3:01
1
1
Maybe it's just me, but I can see nothing in here that tries to select any object by passing some Id somewhere... My guess is that you want to get this mysterious message by its id... but I can see no storage for those messages to begin with. It is completely unclear what you're trying to do.
– Alexander Leonov
Nov 8 '18 at 3:01
Maybe it's just me, but I can see nothing in here that tries to select any object by passing some Id somewhere... My guess is that you want to get this mysterious message by its id... but I can see no storage for those messages to begin with. It is completely unclear what you're trying to do.
– Alexander Leonov
Nov 8 '18 at 3:01
add a comment |
2 Answers
2
active
oldest
votes
@ngrx/entity
doesn't provide a getOneById
selector.
In most of the cases, this is something you shouldn't have to do.
That being said there are ways of creating a selector which does exactly this, for more info see (NgRx: Parameterized selectors)[https://blog.angularindepth.com/ngrx-parameterized-selector-e3f610529f8].
For example:
export const getCount = createSelector(
getCounterValue,
(counter, props) => counter * props.multiply
);
export const selectCustomer = (id: string) => createSelector(
selectCustomers,
customers => customers[id]
);
add a comment |
@ngrx/entity
does provide a getEntityValueById after their release of v6.1.0 (ngrx entity changelog) where they have introduced dictionary
.
Thus using this, I was able to pull a single value of an entity by its id in the below way.
// in the selector
import Dictionary from '@ngrx/entity;
export const getMessageById = () =>
return createSelector(
selectMessagesEntities,
(entities: Dictionary<Message>, props: messageId: number ) =>
return entities[props.messageId];
,
);
;
//to call the selector
this.msg$ = this.store.pipe(select(
fromStore.getMessageById(), messageId: 10
)).pipe(
map(
(message: Message) =>
return this.msg = message;
,
)
);
Also a great reference for this approach has been documented
here.
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',
autoActivateHeartbeat: false,
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%2f53199211%2fangular-ngrx-not-able-to-select-single-value-in-ngrx-entity-based-on-id%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
@ngrx/entity
doesn't provide a getOneById
selector.
In most of the cases, this is something you shouldn't have to do.
That being said there are ways of creating a selector which does exactly this, for more info see (NgRx: Parameterized selectors)[https://blog.angularindepth.com/ngrx-parameterized-selector-e3f610529f8].
For example:
export const getCount = createSelector(
getCounterValue,
(counter, props) => counter * props.multiply
);
export const selectCustomer = (id: string) => createSelector(
selectCustomers,
customers => customers[id]
);
add a comment |
@ngrx/entity
doesn't provide a getOneById
selector.
In most of the cases, this is something you shouldn't have to do.
That being said there are ways of creating a selector which does exactly this, for more info see (NgRx: Parameterized selectors)[https://blog.angularindepth.com/ngrx-parameterized-selector-e3f610529f8].
For example:
export const getCount = createSelector(
getCounterValue,
(counter, props) => counter * props.multiply
);
export const selectCustomer = (id: string) => createSelector(
selectCustomers,
customers => customers[id]
);
add a comment |
@ngrx/entity
doesn't provide a getOneById
selector.
In most of the cases, this is something you shouldn't have to do.
That being said there are ways of creating a selector which does exactly this, for more info see (NgRx: Parameterized selectors)[https://blog.angularindepth.com/ngrx-parameterized-selector-e3f610529f8].
For example:
export const getCount = createSelector(
getCounterValue,
(counter, props) => counter * props.multiply
);
export const selectCustomer = (id: string) => createSelector(
selectCustomers,
customers => customers[id]
);
@ngrx/entity
doesn't provide a getOneById
selector.
In most of the cases, this is something you shouldn't have to do.
That being said there are ways of creating a selector which does exactly this, for more info see (NgRx: Parameterized selectors)[https://blog.angularindepth.com/ngrx-parameterized-selector-e3f610529f8].
For example:
export const getCount = createSelector(
getCounterValue,
(counter, props) => counter * props.multiply
);
export const selectCustomer = (id: string) => createSelector(
selectCustomers,
customers => customers[id]
);
answered Nov 8 '18 at 7:59
timdeschryvertimdeschryver
2,4271110
2,4271110
add a comment |
add a comment |
@ngrx/entity
does provide a getEntityValueById after their release of v6.1.0 (ngrx entity changelog) where they have introduced dictionary
.
Thus using this, I was able to pull a single value of an entity by its id in the below way.
// in the selector
import Dictionary from '@ngrx/entity;
export const getMessageById = () =>
return createSelector(
selectMessagesEntities,
(entities: Dictionary<Message>, props: messageId: number ) =>
return entities[props.messageId];
,
);
;
//to call the selector
this.msg$ = this.store.pipe(select(
fromStore.getMessageById(), messageId: 10
)).pipe(
map(
(message: Message) =>
return this.msg = message;
,
)
);
Also a great reference for this approach has been documented
here.
add a comment |
@ngrx/entity
does provide a getEntityValueById after their release of v6.1.0 (ngrx entity changelog) where they have introduced dictionary
.
Thus using this, I was able to pull a single value of an entity by its id in the below way.
// in the selector
import Dictionary from '@ngrx/entity;
export const getMessageById = () =>
return createSelector(
selectMessagesEntities,
(entities: Dictionary<Message>, props: messageId: number ) =>
return entities[props.messageId];
,
);
;
//to call the selector
this.msg$ = this.store.pipe(select(
fromStore.getMessageById(), messageId: 10
)).pipe(
map(
(message: Message) =>
return this.msg = message;
,
)
);
Also a great reference for this approach has been documented
here.
add a comment |
@ngrx/entity
does provide a getEntityValueById after their release of v6.1.0 (ngrx entity changelog) where they have introduced dictionary
.
Thus using this, I was able to pull a single value of an entity by its id in the below way.
// in the selector
import Dictionary from '@ngrx/entity;
export const getMessageById = () =>
return createSelector(
selectMessagesEntities,
(entities: Dictionary<Message>, props: messageId: number ) =>
return entities[props.messageId];
,
);
;
//to call the selector
this.msg$ = this.store.pipe(select(
fromStore.getMessageById(), messageId: 10
)).pipe(
map(
(message: Message) =>
return this.msg = message;
,
)
);
Also a great reference for this approach has been documented
here.
@ngrx/entity
does provide a getEntityValueById after their release of v6.1.0 (ngrx entity changelog) where they have introduced dictionary
.
Thus using this, I was able to pull a single value of an entity by its id in the below way.
// in the selector
import Dictionary from '@ngrx/entity;
export const getMessageById = () =>
return createSelector(
selectMessagesEntities,
(entities: Dictionary<Message>, props: messageId: number ) =>
return entities[props.messageId];
,
);
;
//to call the selector
this.msg$ = this.store.pipe(select(
fromStore.getMessageById(), messageId: 10
)).pipe(
map(
(message: Message) =>
return this.msg = message;
,
)
);
Also a great reference for this approach has been documented
here.
answered Nov 12 '18 at 3:56
prabhat gundepalliprabhat gundepalli
144114
144114
add a comment |
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%2f53199211%2fangular-ngrx-not-able-to-select-single-value-in-ngrx-entity-based-on-id%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
1
Maybe it's just me, but I can see nothing in here that tries to select any object by passing some Id somewhere... My guess is that you want to get this mysterious message by its id... but I can see no storage for those messages to begin with. It is completely unclear what you're trying to do.
– Alexander Leonov
Nov 8 '18 at 3:01