angular NGRX: Not able to select single value in ngrx entity based on id










1














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.










share|improve this question

















  • 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 at 3:01















1














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.










share|improve this question

















  • 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 at 3:01













1












1








1







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.










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 7 at 23:01









prabhat 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 at 3:01












  • 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 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 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 at 3:01












2 Answers
2






active

oldest

votes


















2














@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]
);





share|improve this answer




























    0














    @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.






    share|improve this answer




















      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
      );



      );













      draft saved

      draft discarded


















      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









      2














      @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]
      );





      share|improve this answer

























        2














        @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]
        );





        share|improve this answer























          2












          2








          2






          @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]
          );





          share|improve this answer












          @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]
          );






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 8 at 7:59









          timdeschryver

          2,2021110




          2,2021110























              0














              @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.






              share|improve this answer

























                0














                @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.






                share|improve this answer























                  0












                  0








                  0






                  @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.






                  share|improve this answer












                  @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.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 12 at 3:56









                  prabhat gundepalli

                  144114




                  144114



























                      draft saved

                      draft discarded
















































                      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.




                      draft saved


                      draft discarded














                      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





















































                      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







                      這個網誌中的熱門文章

                      How to read a connectionString WITH PROVIDER in .NET Core?

                      Node.js Script on GitHub Pages or Amazon S3

                      Museum of Modern and Contemporary Art of Trento and Rovereto