Json-server: Many-to-many relationship causes "?_expand" throws 'Cannot read property 'toString' of undefined'

Created on 26 Jul 2018  路  3Comments  路  Source: typicode/json-server

I'm trying to write a site where a user can keep track of the TV shows they watch. Pretty simple, eh?

This simplest thing I need to so is show a list of shows the logged in user has watched, and the details of what they've watched. It's a simple many-to-many relationship. Many users watch many shows.

Users <-> Watches <-> Shows

I have a simple DB format;

{
  "users": [{
    "id": 0,
    "name": "Jeff"
  },
  {
    "id": 1,
    "name": "Boris"
  }],
  "watches": [
  {
    "id":0,
    "userId":0,
    "showId":0,
    "complete":true
   },
   {
     "id":1,
     "userId":0,
     "showId":1,
     "complete":false
   },
   {
     "id":3,
     "userId":1,
     "showId":0,
     "complete":true
   }
 ],
 "shows": [
 {
   "id": 0,
   "name": "Battlestar Galactica"
 },
 {
   "id": 1,
   "name": "Firefly"
 },
 {
   "id": 2,
   "name": "Stargate SG-1"
 }
 ]
}

In this example we have 2 users, one user has watched two shows, the 2nd user has watched one show. They share shows, so one show goes unwatched.

So, here are queries and what I expect, what I receive, so you can tell me where I am going wrong.

/users/0
expected: the details of 'Jeff'.
actual: this works!

/users/0/watches
expected: the 'watches' of the user 'Jeff'
actual: again, this works - I get two watches objects that are linked to 'Jeff'

/users/0/shows
expected: the 'shows' that the user 'Jeff' has watched
actual: all the shows. Obviously I am missing something here, or the relationships are too deep for the server to understand. Why the 'user/0' bit becomes superfluous as this is the same as running '/shows', I have no idea.

/users/0/watches/shows
expected: the 'shows' that the user 'Jeff' has watched
actual: nothing. Way too deep for the server to understand. Fair enough.

/users/0/watches/?_expand=shows
expected: the 'watches' of the user 'Jeff', with show details
actual: TypeError: Cannot read property 'toString' of undefined at C:\Users\col\AppData\Roaming\npmnode_modulesjson-servernode_modules\lodash-id\src\index.js:37:51

This is frustrating as this is probably what I really need, and if not this, then the other way round is...

/users/0/shows/?_embed=watches
expected: the 'shows' of the user 'Jeff', with 'watches' details
actual: obviously what I'm really aiming for here... but unfortunately, it comes with all the shows... with the watches embedded, that are linked to the user. This is a close as I can get. It's nearly there. Hell, if it didn't attach all the shows it'd be perfect. The problem comes when we have 1000 shows in the DB, not 3 and the performance dies, due to tons of superfluous information.

Can someone help me here? I appreciate I'm probably doing something very stupid, or missing the point somewhere.

Most helpful comment

@zenith77 hey, i got lucky today, i face the same problem with you, and after i modified a little bit, i found it work

this one is yours
/users/0/watches/?_expand=shows

try this
/users/0/watches/?_expand=show

All 3 comments

i also face the same problem, this bug really need to be solved, been using this json server for many projects, SMH

@zenith77 hey, i got lucky today, i face the same problem with you, and after i modified a little bit, i found it work

this one is yours
/users/0/watches/?_expand=shows

try this
/users/0/watches/?_expand=show

@zenith77 hey, i got lucky today, i face the same problem with you, and after i modified a little bit, i found it work

this one is yours
/users/0/watches/?_expand=shows

try this
/users/0/watches/?_expand=show

Thanks you! nice solution

Was this page helpful?
0 / 5 - 0 ratings

Related issues

fishenal picture fishenal  路  3Comments

jasonlimantoro picture jasonlimantoro  路  4Comments

Isanderthul picture Isanderthul  路  3Comments

TXRRNT picture TXRRNT  路  4Comments

melnikovic picture melnikovic  路  3Comments