ngrx/data – alternate id `has a missing or invalid entity key (id)`

Published

As there are many IDs in the system, my entities linked to database tables and the tables use ‘DbId’ as the primary key – instead of the ngrx/data default: ‘id’.

That is addressed inside the EntityMetadata map by the property: selectId.
All appears working fine, except when adding records to db table.

The addition to the db table proceeds, but after, on the client site, ngrx/data generates error: has a missing or invalid entity key (id) by the: EntityActionGuard.

Here are the relevant code snippets:

                    /**
                     * Authority roles assigned to user
                     * 
                     */
export class UserRolesEntity {
  DbId?: number;
  DbId_User?: number;
  DbId_Role?: number;
  Timestamp?: Date;
}

// - - - - - - - - - - - - - - - - - - - - - - - - - -

export const entityMetadata: EntityMetadataMap = {
. . .
User_Roles: { selectId: (user_roles: UserRolesEntity) => user_roles.DbId },
. . . 
}

.

class NgrxDataService_UR<User_Roles> extends EntityCollectionServiceBase<User_Roles>

.
IN COMPONENT:

constructor(dsUR: NgrxDataService_UR) { . . . }
. . .
fn() {
  const addItem: UserRolesEntity = {
    DbId_User: 3,
    DbId_Role: 11
  }
  this.dsUR.add(addItem);
}

The step creates entry in the database table. Back on client, in debugger, I see NgRx/data utilizes class: EntityActionGuard and fn: mustBeEntity(action) { . . .}

My returned action looks like this:

>payload:
correlationId: "CRID14"
data: Array(1)
>0: {DbId: 29,               <<<----
     DbId_Role: 11,
     DbId_User: 3}
[[Prototype]]: Object
length: 1
[[Prototype]]: Array(0)
entityName: "User_Roles"
entityOp: "@ngrx/data/save/add-one/success"
isOptimistic: false
[[Prototype]]: Object
type: "[User_Roles] @ngrx/data/save/add-one/success"

Note the property DbId in data – it is there with a value !
Still the fn: inside mustBeEntity(action) returns undefined !

const id = this.selectId(data);
// id is undefined 

What may be wrong here ?

Source: Angular Questions

Published
Categorized as angular, angular-ngrx-data, ngrx Tagged , ,

Answers

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Still Have Questions?


Our dedicated development team is here for you!

We can help you find answers to your question for as low as 5$.

Contact Us
faq