Welcome to the Linux Foundation Forum!

why i am getting this err

Posts: 5
edited November 2023 in LFW212 Class Forum

i am trying to solve lab 6.1
but I am getting this error after running node validate.js:

  1. ☑️ GET http://localhost:63883/boat/1 responded with 200 response
  2. ☑️ GET http://localhost:63883/boat/1 responded with correct Content-Type header
  3. ☑️ GET http://localhost:63883/boat/1 responded with correct data
  4. ☑️ GET http://localhost:63883/unsupported/route responded with 404 response
  5. Error: socket hang up
  6. at connResetException (node:internal/errors:720:14)
  7. at Socket.socketOnEnd (node:_http_client:525:23)
  8. at Socket.emit (node:events:526:35)
  9. at endReadableNT (node:internal/streams/readable:1359:12)
  10. at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
  11. code: 'ECONNRESET'
  12. }
  13.  
  14.  
  15. my solution is:
  16.  
  17. "use strict";
  18.  
  19. const { boat } = require('../../model');
  20.  
  21.  
  22. const boatBodySchema = {
  23. type: 'object',
  24. required: ['data'],
  25. additionalProperties: false,
  26. properties: {
  27. data: {
  28. type: 'object',
  29. required: ['brand', 'color'],
  30. additionalProperties: false,
  31. properties: {
  32. brand: { type: 'string' },
  33. color: { type: 'string' }
  34. }
  35. }
  36. }
  37. }
  38. module.exports = async function(fastify, opts) {
  39. fastify.post('/',{schema: {body: boatBodySchema}}, (request, reply) => {
  40. reply.headers('Content-Type', 'application/json');
  41. const {data} = request.body;
  42. const id = boat.uid();
  43. boat.create(id, data, (err, result) => {
  44. if (err) {
  45. console.log(err)
  46. reply.send(err)
  47. } else {
  48. console.log(result)
  49. reply.status(201);
  50. reply.send(id)
  51. }
  52. })
  53. })
  54. fastify.get('/:id', (request, reply) => {
  55. const { id } = request.params;
  56. boat.read(id, (err, result) => {
  57. if (err) {
  58. if (err.message === 'not found') {
  59. reply.status(404);
  60. reply.send(err.message)
  61. } else {
  62. reply.send(err.message)
  63. }
  64. } else {
  65. reply.send(result);
  66. }
  67. })
  68. })
  69.  
  70.  
  71. }

Answers

  • Posts: 160

    it's your error handling, the next check checks for 500 status code and the if/else conditional breaks that. as an aside: don't check against err.message, use code

Welcome!

It looks like you're new here. Sign in or register to get started.
Sign In

Welcome!

It looks like you're new here. Sign in or register to get started.
Sign In

Categories

Upcoming Training