typicode / json-server

Get a full fake REST API with zero coding in less than 30 seconds (seriously)
Other
72.79k stars 7.01k forks source link

POST failing when running json server via npm run #303

Open hadynz opened 8 years ago

hadynz commented 8 years ago

I have json server configured to run conveniently using a npm-run-script like so npm run db.

{
  "name": "My App",
  "scripts": {
    "db": "json-server --watch db.json --port 3004"
  }
}

This works fine for all GET requests, however, fails for any POST request with the following stacktrace:

TypeError: _.maxBy is not a function
    at Function.createId (/Users/hady.osman/Development/react-native/PeopleNote/node_modules/json-server/src/server/mixins.js:44:16)
    at Function.module.exports.insert (/Users/hady.osman/Development/react-native/PeopleNote/node_modules/underscore-db/src/index.js:58:31)
    at baseWrapperValue (/Users/hady.osman/Development/react-native/PeopleNote/node_modules/lodash/index.js:2768:30)
    at LodashWrapper.wrapperValue (/Users/hady.osman/Development/react-native/PeopleNote/node_modules/lodash/index.js:6124:14)
    at LodashWrapper.<anonymous> (/Users/hady.osman/Development/react-native/PeopleNote/node_modules/lowdb/lib/_index.js:76:19)
    at LodashWrapper.wrapper [as value] (/Users/hady.osman/Development/react-native/PeopleNote/node_modules/lodash/index.js:3592:19)
    at create (/Users/hady.osman/Development/react-native/PeopleNote/node_modules/json-server/src/server/router/plural.js:214:8)
    at Layer.handle [as handle_request] (/Users/hady.osman/Development/react-native/PeopleNote/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/hady.osman/Development/react-native/PeopleNote/node_modules/express/lib/router/route.js:131:13)
    at next (/Users/hady.osman/Development/react-native/PeopleNote/node_modules/express/lib/router/route.js:125:14)

This is not a problem if I was to run the json-server command manually on the command line using the very same db.json that I have.

Seems like there is a problem with the scope that is used to run the script?

typicode commented 8 years ago

Hi @hadynz,

That's strange, there are tests that cover POST requests (using the CLI and the module). So as a quick test, I would compare versions:

./node_modules/.bin/json-server -v # this is the bin used in npm run
json-server -v # the globally installed bin

I would also delete node_modules, check that json-server version in package.json is the latest and reinstall everything npm install.

Let me know if you still have the issue.

hadynz commented 8 years ago

Bizarre results.

First of all, both global and local versions were identical.

➜  PeopleNote git:(sagas-normalizr) ✗ json-server -v
0.8.14
➜  PeopleNote git:(sagas-normalizr) ✗ ./node_modules/.bin/json-server -v
0.8.14

I deleted my local version rm -rf ./node_modules/.bin/json-server and rm -rf ./node_modules/json-server.

Running npm run db then worked correctly for POST. I think this happened because the global version of json-server was the one that run; just like me running it manually from command line.

I reinstalled the module locally by doing an npm install, and sure enough ./node_modules/.bin/json-server was back there. Ran npm run db and tried to do a POST and got the very same _.maxBy error that I pasted above.

andrewsuzuki commented 8 years ago

Same issue here. I deleted node_modules then ran a fresh npm install, which yielded the npm warning

npm WARN lowdb@0.13.1 requires a peer of lodash@4 but none was installed.

I ran npm install --save lodash to fix the issue.

Pines-Cheng commented 8 years ago

I meet the problem too

typicode commented 8 years ago

That's strange, since lodash is already a dependency of json-server: https://github.com/typicode/json-server/blob/master/package.json#L19

romain10009 commented 8 years ago

+1 on that issue, I see it is using lodash 3.10.x in json-server/src/server/mixins.js although the lodash that is installed locally in node_modules/json-server/node_modules/lodash is 4.13.1

hasanmumin commented 8 years ago

+1 i don't have this problem with the previous version.

TypeError: _.maxBy is not a function
    at Function.createId (/Users/hasanmumin/Development/robe-react-ui/node_modules/json-server/src/server/mixins.js:44:16)
    at Function.module.exports.insert (/Users/hasanmumin/Development/robe-react-ui/node_modules/underscore-db/src/index.js:58:31)
    at baseWrapperValue (/Users/hasanmumin/Development/robe-react-ui/node_modules/lodash/index.js:2768:30)
    at LodashWrapper.wrapperValue (/Users/hasanmumin/Development/robe-react-ui/node_modules/lodash/index.js:6124:14)
    at LodashWrapper.<anonymous> (/Users/hasanmumin/Development/robe-react-ui/node_modules/lowdb/lib/_index.js:77:19)
    at LodashWrapper.wrapper [as value] (/Users/hasanmumin/Development/robe-react-ui/node_modules/lodash/index.js:3592:19)
    at create (/Users/hasanmumin/Development/robe-react-ui/node_modules/json-server/src/server/router/plural.js:217:8)
    at Layer.handle [as handle_request] (/Users/hasanmumin/Development/robe-react-ui/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/hasanmumin/Development/robe-react-ui/node_modules/express/lib/router/route.js:131:13)
    at next (/Users/hasanmumin/Development/robe-react-ui/node_modules/express/lib/router/route.js:125:14)
    at Route.dispatch (/Users/hasanmumin/Development/robe-react-ui/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/Users/hasanmumin/Development/robe-react-ui/node_modules/express/lib/router/layer.js:95:5)
    at /Users/hasanmumin/Development/robe-react-ui/node_modules/express/lib/router/index.js:277:22
    at Function.process_params (/Users/hasanmumin/Development/robe-react-ui/node_modules/express/lib/router/index.js:330:12)
    at next (/Users/hasanmumin/Development/robe-react-ui/node_modules/express/lib/router/index.js:271:10)
    at Function.handle (/Users/hasanmumin/Development/robe-react-ui/node_modules/express/lib/router/index.js:176:3)
helio-frota commented 8 years ago

+1 here

npm install lodash -D solve my problem.

Meligy commented 8 years ago

I can confirm @helio-frota's workaround works for me too.

aecz commented 8 years ago

Looks like it has to do with initializing low-db. I think we should pass the instance of lodash, otherwise low-db will take the lodash found at the root of the project (which can have a version lower than 4 or worse, no version at all).

typicode commented 7 years ago

It may be worth rechecking with JSON Server v0.9.0. In lowdb, lodash is now a dependency instead of a peerDependency.