React collectionDataStore

Hello,
I have been using REACT for a short time. Now, I have specific needs and I cannot find examples to move forward.
My need is the following:
I have a “Person” entity with the attributes “name” and “Firstname”…
I want to make a request to find a single record by searching on the “name” attribute (example name = ‘Kelly’.
I looked at what I could do with dataInstanceStore but I can’t do it without passing it the ID of the record I’m looking for (which I don’t know in my context).
By using collectionDataStore, I manage to pass the filter on the “name” attribute to it, but I have trouble exploiting the result. Indeed, I do not know how to access the values ​​of the attributes.
Can you confirm the correct method for me and give me an example if you have one.
With thanks

dataCollection = collection<Person>(Person.NAME,{
    view: "personne-view-list",
    filter: {conditions: [{property: 'name', operator: "contains", value: 'Kelly'}]},
    limit: 1,
    loadImmediately: true
  })
...
render() {
    const { status, items, count } = this.dataCollection;
...

Hello:

It appears you never got an answer. I have a very similar problem. I’ve done the query on the collection and received at least one item back. I know because I’ve logged the id field and the log shows a value. I’ve tried adding an instance for the record I actually want and then a load call on that instance (Webuser), but it does not work either.

         if (this.webUsers.items[0].id != null) {
            this.webUser = instance<Webusers>(Webusers.NAME, {
              view: "_local",
              loadImmediately: false
            });
            console.log("ID to load is:  " + this.webUsers.items[0].id);
            this.webUser.load(this.webUsers.items[0].id);
         }

Maybe someone can show us both how to do this?

Hi @strai1.mail,

You can access the attributes like this:

items[0]?.name

Hi @eraskin,

Your approach should work. Could you please share a minimal example reproducing the issue?

Not sure I can isolate this code from the issue, but I’m getting closer. FYI, this is version 7.2.10 of the platform. I’m still not getting the entire entity loaded. It seems like it is loading “_minimal” rather than “_local”, even though I specify that as the view. Here’s the entity code:

export class Webusers extends BaseIdentityIdEntity {
  static NAME = "pasweb_Webusers";
  uuid?: any | null;
  login?: string | null;
  password?: string | null;
  email?: string | null;
  usertype?: WebUserTypes | null;
  customer?: Customers | null;
  mailer?: Mailers | null;
  custpage?: string | null;
  broker?: Brokers | null;
  owner?: Customers | null;
  salesrep?: Salesreps | null;
}
export type WebusersViewName = "_base" | "_local" | "_minimal";
export type WebusersView<V extends WebusersViewName> = V extends "_base"
  ? Pick<
      Webusers,
      "id" | "login" | "password" | "email" | "usertype" | "custpage"
    >
  : V extends "_local"
  ? Pick<
      Webusers,
      "id" | "login" | "password" | "email" | "usertype" | "custpage"
    >
  : never;

In the debugger, here’s my view of the loaded entity (after the load statement and a status of “DONE”):

image

As you can see, there are no fields in the object. The code that loaded it is now:

  @action
  doWebuserLookup(userName: string | undefined) {
    this.performingWebuserLookup = true;
    this.webUsers = collection<Webusers>(Webusers.NAME, {
      view: "_minimal",
      filter: {
        conditions: [
          {property: "login", operator: "=", value: userName || null}
        ]
      },
      loadImmediately: false
    })
    this.webUsers.load()
      .then(
        action(() => {
          const {id} = this.webUsers.items[0];
          if (id != null) {
            this.webUser = instance<Webusers>(Webusers.NAME, {
              view: "_local",
              loadImmediately: false
            });
            console.log("ID to load is:  " + id);
            this.webUser.load(id);
            this.reactionDisposer = reaction(
              () => { return this.webUser.item },
              () => {
                this.performingWebuserLookup = false;
              }
            )
          }
        })
      )
      .catch(
        action(() => {
          this.performingWebuserLookup = false;
          message.error(this.props.intl.formatMessage({ id: "secureArea.userNotFound" }));
        })
      );
  }

I filter in a collection, then load the first item in the collection. That’s the only way I’ve found to get a search on a non-ID field to complete. I did try view: “_local” on the collection, but it did not load the entire view either. Obviously I would like to remove the second round trip.

Why are my fields not loading based on the view?

I will try to generate a minimal example, but I really am a noob at React so I’m not sure I can.

I have managed to create a minimal project to illustrate my issue: webusertest.zip (2.3 MB)

When executed, I get this result after all loading is complete:

image

As you can see, the view is “_local”, the status is “DONE”, but the items do not have all the fields.

What am I doing wrong?

One more bit of info. Network response json contains this (I added the line breaks for readability):

[{"_entityName":"webusertest_Webusers",
  "_instanceName":"com.company.webusertest.entity.Webusers-4 [detached]",
  "id":"4"}
]

As you can see, I only got back the ID and no other fields. To test, I added an instance name to the webusers entity and tried again. My json came back the same, so it is not even including the instance name.

Is this the way it is supposed to work? Collections only return IDs and not the entire view? That makes no sense or your browse windows wouldn’t work, right?

I did do an “npm run update-model” to make sure my model was updated. I did not see any change in the entity .ts file. How does it even know the instance name? But that’s a question for another day…

Does anybody have any idea why this isn’t working?

I think I finally solved this. In order to get the attributes of the entities, I had to go into the Security Roles and create a rest-user-access Role. In that Role, I had to go into the Entities tab and click the Read checkbox in the upper right - Allow all Entities. That got me access to the entities, but NOT THEIR ATTRIBUTES!

I then had to go into the Attributes tab and click the Read-Only checkbox in the Allow all attributes box on the upper right.

Finally, I assigned this Role to my user.

I now receive all the attributes in the view.