json-server
json-server copied to clipboard
find a bug about realtionId
this is my db:
{
"users": [],
"posts": []
}
then I create a user:
POST http://localhost:3000/users
{
"name": "aaa"
}
now db like this:
then i create a post by user's id:
POST http://localhost:3000/users/1/posts
{
"name": "post1"
}
now db is:
I can't get post embed into user with
_embed
, because userId
is a string
:
if I change the
userId
to int
,it works:
so I think it's a bug.
@YouWillBe I would say it is not a pure bug as it is an inherited behaviour from lodash APIs.
json-server uses lowdb .. lowdb uses lodash APIs, specifically in this case, the APIs it uses is filter
here
Giving the behavior of lodash isMatch
function, it uses strict equal ===
to check values equality when filter using _.matches
iteratee shorthand`
For example.. open lodash documentation page and use your developer tools to run the following to see the difference.
var users = [
{ 'user': 'barney', 'age': 36, 'active': true },
{ 'user': 'fred', 'age': 40, 'active': false }
];
_.filter(users, { 'age': 36, 'active': true });
_.filter(users, { 'age': '36', 'active': true });
On the otherhand, I think it could be solve by using custom iteratee filter function
from
const query = {}
const singularResource = pluralize.singular(name)
query[`${singularResource}${opts.foreignKeySuffix}`] = resource.id
to
const singularResource = pluralize.singular(name)
const query = (subResource) => subResource[`${singularResource}${opts.foreignKeySuffix}`] == resource.id
I didn't test it, however it should work.
@typicode what do you think? I can push PR with it if you are ok with that.
Any update on this? I think this should be a must have feature.
Any update on this? I think this should be a must have feature.
Still like this. I think i should reopen it.