feathers icon indicating copy to clipboard operation
feathers copied to clipboard

Returning `undefined` from service's method, always get 404 HTTP Status instead of 204

Open Thammachart opened this issue 5 years ago • 3 comments

Steps to reproduce

I've created an reproducible repo here: https://github.com/Thammachart/feathersjs-204-issue

  • Created the feathersjs Project using feathers g app, with @feathersjs/cli version 4.2.4
    • Choices were Javascript, Yarn, REST API only, Mocha + Assert, No Authentication
  • Created Service, in the example repo, svc1
    • Choices were A custom service, svc1, /svc1
  • Created service's methods for multiple cases, see the link
  • Run yarn start
  • Using curl localhost:3030/svc1 with multiple flags like -XGET, -XPOST, -XPATCH, -XDELETE to test each case in each service's method

Expected behavior

  • Any methods that return undefined or null should response with 204 No Content HTTP Status Code

Actual behavior

  • methods returning undefined implicitly or explicitly, response with the 404 Not Found HTTP Status Code, even though the method exists and got to run properly (console.log in methods run properly)
  • methods returning null work as expected
  • Interesting Note 1: methods returning boolean false response with 204 No Content HTTP Status Code
  • Interesting Note 2: methods returning boolean true response with 2xx HTTP Status Code with content of literal true

I don't know if there is underlying convention behind this. I've checked the Docs, FAQ to no avail.

System configuration

Module versions (especially the part that's not working):

  • @feathersjs/feathers ^4.3.11
  • @feathersjs/express ^4.3.11
  • The rest can be seen at this link

NodeJS version: 12.13.1 from nvm

Operating System: Arch Linux, Kernel 5.4.3-arch1-1

Browser Version: curl 7.67.0

Thammachart avatar Dec 17 '19 08:12 Thammachart

Yes, the return value should be null. It's tested at https://github.com/feathersjs/feathers/blob/master/packages/express/test/rest.test.js#L519 (service at https://github.com/feathersjs/feathers/blob/master/packages/express/test/rest.test.js#L467). Not entirely sure where the best place for it would be in the docs.

daffl avatar Dec 17 '19 18:12 daffl

Ok then,

  • what about undefined? Should it be treated differently than null ? For me, in the context of response payload, there is no difference, and undefined is easily a pitfall, when you forget to return
  • what about boolean true/false inconsistency?

Thammachart avatar Dec 18 '19 07:12 Thammachart

Is this behavior still happen ? should we return 'undefined' or 'void' ? only 'null' ?

dor-benatia avatar Mar 31 '22 08:03 dor-benatia