{"action":"created","issue":{"url":"https://api.github.com/repos/typicode/json-server/issues/825","repository_url":"https://api.github.com/repos/typicode/json-server","labels_url":"https://api.github.com/repos/typicode/json-server/issues/825/labels{/name}","comments_url":"https://api.github.com/repos/typicode/json-server/issues/825/comments","events_url":"https://api.github.com/repos/typicode/json-server/issues/825/events","html_url":"https://github.com/typicode/json-server/issues/825","id":344812356,"node_id":"MDU6SXNzdWUzNDQ4MTIzNTY=","number":825,"title":"Many-to-many relationship causes \"?_expand\" throws 'Cannot read property 'toString' of undefined'","user":{"login":"zenith77","id":2838850,"node_id":"MDQ6VXNlcjI4Mzg4NTA=","avatar_url":"https://avatars2.githubusercontent.com/u/2838850?v=4","gravatar_id":"","url":"https://api.github.com/users/zenith77","html_url":"https://github.com/zenith77","followers_url":"https://api.github.com/users/zenith77/followers","following_url":"https://api.github.com/users/zenith77/following{/other_user}","gists_url":"https://api.github.com/users/zenith77/gists{/gist_id}","starred_url":"https://api.github.com/users/zenith77/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/zenith77/subscriptions","organizations_url":"https://api.github.com/users/zenith77/orgs","repos_url":"https://api.github.com/users/zenith77/repos","events_url":"https://api.github.com/users/zenith77/events{/privacy}","received_events_url":"https://api.github.com/users/zenith77/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2018-07-26T11:54:13Z","updated_at":"2018-09-18T10:40:04Z","closed_at":null,"author_association":"NONE","body":"I'm trying to write a site where a user can keep track of the TV shows they watch. Pretty simple, eh?\r\n\r\nThis 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.\r\n\r\nUsers <-> Watches <-> Shows\r\n\r\nI have a simple DB format;\r\n\r\n{\r\n  \"users\": [{\r\n    \"id\": 0,\r\n    \"name\": \"Jeff\"\r\n  },\r\n  {\r\n    \"id\": 1,\r\n    \"name\": \"Boris\"\r\n  }],\r\n  \"watches\": [\r\n  {\r\n    \"id\":0,\r\n    \"userId\":0,\r\n    \"showId\":0,\r\n    \"complete\":true\r\n   },\r\n   {\r\n     \"id\":1,\r\n     \"userId\":0,\r\n     \"showId\":1,\r\n     \"complete\":false\r\n   },\r\n   {\r\n     \"id\":3,\r\n     \"userId\":1,\r\n     \"showId\":0,\r\n     \"complete\":true\r\n   }\r\n ],\r\n \"shows\": [\r\n {\r\n   \"id\": 0,\r\n   \"name\": \"Battlestar Galactica\"\r\n },\r\n {\r\n   \"id\": 1,\r\n   \"name\": \"Firefly\"\r\n },\r\n {\r\n   \"id\": 2,\r\n   \"name\": \"Stargate SG-1\"\r\n }\r\n ]\r\n}\r\n\r\nIn 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.\r\n\r\nSo, here are queries and what I expect, what I receive, so you can tell me where I am going wrong.\r\n\r\n/users/0\r\nexpected: the details of 'Jeff'.\r\nactual: this works!\r\n\r\n/users/0/watches\r\nexpected: the 'watches' of the user 'Jeff'\r\nactual: again, this works - I get two watches objects that are linked to 'Jeff'\r\n\r\n/users/0/shows\r\nexpected: the 'shows' that the user 'Jeff' has watched\r\nactual: 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.\r\n\r\n/users/0/watches/shows\r\nexpected: the 'shows' that the user 'Jeff' has watched\r\nactual: nothing. Way too deep for the server to understand. Fair enough.\r\n\r\n/users/0/watches/?_expand=shows\r\nexpected: the 'watches' of the user 'Jeff', with show details\r\nactual: TypeError: Cannot read property 'toString' of undefined at C:\\Users\\col\\AppData\\Roaming\\npm\\node_modules\\json-server\\node_modules\\lodash-id\\src\\index.js:37:51\r\n\r\nThis is frustrating as this is probably what I really need, and if not this, then the other way round is...\r\n\r\n/users/0/shows/?_embed=watches\r\nexpected: the 'shows' of the user 'Jeff', with 'watches' details\r\nactual: 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.\r\n\r\nCan someone help me here? I appreciate I'm probably doing something very stupid, or missing the point somewhere.\r\n"},"comment":{"url":"https://api.github.com/repos/typicode/json-server/issues/comments/422344231","html_url":"https://github.com/typicode/json-server/issues/825#issuecomment-422344231","issue_url":"https://api.github.com/repos/typicode/json-server/issues/825","id":422344231,"node_id":"MDEyOklzc3VlQ29tbWVudDQyMjM0NDIzMQ==","user":{"login":"sanBastia","id":23252753,"node_id":"MDQ6VXNlcjIzMjUyNzUz","avatar_url":"https://avatars0.githubusercontent.com/u/23252753?v=4","gravatar_id":"","url":"https://api.github.com/users/sanBastia","html_url":"https://github.com/sanBastia","followers_url":"https://api.github.com/users/sanBastia/followers","following_url":"https://api.github.com/users/sanBastia/following{/other_user}","gists_url":"https://api.github.com/users/sanBastia/gists{/gist_id}","starred_url":"https://api.github.com/users/sanBastia/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sanBastia/subscriptions","organizations_url":"https://api.github.com/users/sanBastia/orgs","repos_url":"https://api.github.com/users/sanBastia/repos","events_url":"https://api.github.com/users/sanBastia/events{/privacy}","received_events_url":"https://api.github.com/users/sanBastia/received_events","type":"User","site_admin":false},"created_at":"2018-09-18T10:40:04Z","updated_at":"2018-09-18T10:40:04Z","author_association":"NONE","body":"@zenith77 hey, i got lucky today, i face the same problem with you, and after i modified a little bit, i found it work \r\n\r\nthis one is yours\r\n`/users/0/watches/?_expand=shows`\r\n\r\ntry this \r\n`/users/0/watches/?_expand=show`\r\n\r\n"}}
{"actor":{"display_login":"sanBastia"}}