Welcome to the Linux Foundation Forum!

why i am getting this err

Options
ibrahimtech
ibrahimtech 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:

☑️  GET http://localhost:63883/boat/1 responded with 200 response
☑️  GET http://localhost:63883/boat/1 responded with correct Content-Type header
☑️  GET http://localhost:63883/boat/1 responded with correct data
☑️  GET http://localhost:63883/unsupported/route responded with 404 response
Error: socket hang up
    at connResetException (node:internal/errors:720:14)
    at Socket.socketOnEnd (node:_http_client:525:23)
    at Socket.emit (node:events:526:35)
    at endReadableNT (node:internal/streams/readable:1359:12)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
  code: 'ECONNRESET'
}


my solution is: 

"use strict";

const { boat } = require('../../model');


const boatBodySchema = {
    type: 'object',
    required: ['data'],
    additionalProperties: false,
    properties: {
        data: {
            type: 'object',
            required: ['brand', 'color'],
            additionalProperties: false,
            properties: {
                brand: { type: 'string' },
                color: { type: 'string' }
            }
        }
    }
}
module.exports = async function(fastify, opts) {
    fastify.post('/',{schema: {body: boatBodySchema}}, (request, reply) => {
        reply.headers('Content-Type', 'application/json');
        const {data} = request.body;
        const id = boat.uid();
        boat.create(id, data, (err, result) => {
            if (err) {
                console.log(err)
                reply.send(err)
            } else  {
                console.log(result)
                reply.status(201);
                reply.send(id)
            }
        })
    })
    fastify.get('/:id', (request, reply) => {
        const { id } = request.params;
        boat.read(id, (err, result) => {
            if (err) {
                if (err.message === 'not found') {
                    reply.status(404);
                    reply.send(err.message)
                } else {
                    reply.send(err.message)
                }
            } else  {
                reply.send(result);
            }
        })
    })


}

Answers

  • xdxmxc
    xdxmxc Posts: 148
    Options

    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

Categories

Upcoming Training