ForestAdmin / forest-express

🧱 Dependency of Express Lianas for Forest Admin

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Having trouble getting hasMany smart fields to work.

gcoombe opened this issue · comments

According to the documentation for a hasMany smart field I can define my own route in the form:

router.get('/forest/actor/:actorId/relationships/movies', topMovies);

When I do this my route never gets hit and I see an error being thrown from within forest-express. Looking through the code I see this:
https://github.com/ForestAdmin/forest-express/blob/devel/routes/associations.js#L89

Maybe I'm missing something but isn't that route on the same path as the example one in the docs? If so that would explain why my route never gets hit since the request matches the once noted above.

I feel like I'm probably missing something but I can't for the life of me get a smart field with hasMany to work as is in the documentation. Is there a forest-express example somewhere that I could take a look at?

Thanks!

Hey @gcoombe, did you defined the Smart Field for your collection this way (1st step of the documentation):

'use strict';
var Liana = require('forest-express-sequelize');

Liana.collection('actors', {
  fields: [{
    field: 'topmovies',
    type: ['String'],
    reference: 'movies.id'
  }]
});

Obviously you need to adapt it with your own collection name.

Yes I did do that.

Ok @gcoombe, so you can see, in the Forest UI, the HasMany Smart Field you defined, right?

You might be right about the issue 😞
If you define your route router.get('/forest/actor/:actorId/relationships/movies', topMovies); after the Forest integration in your code, you might have conflicts with routes generated by Forest (that's what you mentioned). Can you try to declare your HasMany Smart Field route before the Forest integration?

Hey @arnaudbesnier , yes I can see the smartField. I think the issue is conflicting routes. AFAIK with express if you define handlers for the same route multiple times the 1st one is called. I don't know of a good way of overriding the route later on.

I can define it before the integration and that works but then I lose the ability to use the ensureAuthenticated middleware :(

@gcoombe then this is a bug we need to fix

hey @gcoombe, the issue is fixed in the latest releases. Everything should work as expected.

Please re-open the issue, if your issue persists after the liana upgrade.

🌲🌲🌲