SoftwareBrothers / adminjs

AdminJS is an admin panel for apps written in node.js
https://adminjs.co
MIT License
8.21k stars 661 forks source link

Sequelize – resource not read #311

Closed mb8z closed 4 years ago

mb8z commented 4 years ago

I have few one to many relationships defined and AdminBro is pretty good at rendering the reference fields for those, but today I created another model called Offer and it belongs to Coupon (while Coupon has many Offer). The COUPON ID field is not rendered when I try to add the Offer in the admin-bro dashboard.

I have another relationship like this (Cart belongs to Coupon and Coupon has many Cart) and the COUPON ID field is visible when I try to add Cart in the dashboard. I checked my database and the columns seem to be the same etc.

My first question – why is this happening? My second question – how can I manually add the reference field? I have tried adding it manually but am stucked:

{
  resource: DB.Offer,
  options: {
    properties: {
      coupon_id: {
         type: 'reference',
         // What to put here?
      },
    },
  },
},
wojtek-krysiak commented 4 years ago

Hmm. you can open a chrome terminal and check out what is in the ReduxStore. There is a global variable REDUX_STATE which holds all the values. Then navigate to resources and see which editProperties are assigned for an Offer and whether the coupon_id is among them (but remove options.properties.coupon_id from your ResourceOptions first).

If not - probably admin-bro-mongoose has problem with parsing your entity. Maybe you forgot to run the migrations? - hard to tell.

You can write also a before hook for edit action and see what is in context.resource.properties() (3rd parameter).

Please let me know what was the result.

Regarding the "manual reference field" it is not possible this way. You have to create new property and then write custom component which will handle that. But if AdminBro doesn't have your property you will have to populate the database manually (== lots of work).

So please try debug this in the way I suggested and let me know what was the result. And we will try to move it from there.

mb8z commented 4 years ago

Thanks for the fast response – just to clarify – I'm using admin-bro-sequelizejs, not the mongoose one.

I've checked the REDUX_STATE and the Cart has e.g. the coupon_id in the listProperties object, but the Offer does not. I've also checked how my database and its structure to confirm the migrations were run and this is the structure I got for the Offer:

Screenshot 2020-02-26 at 13 22 21

And that's the one I got for the Cart:

Screenshot 2020-02-26 at 13 23 10

Is there maybe any cache of the resources that I can clean? I guess the params are parsed with parseParams method of the adapter – any idea how can I maybe track what is parsed and what's not? The only difference I see is that the coupon_id for the Offer property was added in new migration.

mb8z commented 4 years ago

Ok, got this figure out – the admin-bro was not the fault. The boilerplate I'm using was having async function that associated models, and the async/await is not needed at all there. I removed it and it started to work. Hard-to-track issue as the whole app worked perfectly 😞

Maybe it will help someone! Closing this – once again, thanks for your fast response!