0

I have a post request that submits a patient name and the server is supposed to give me back patient_id in response. I get a 200 response back to the client however I don't get the patient_id back which is what I need. When I console log on the server i can see patient.id is generated and there are no errors either. Wonder if there is something I am missing?

Response - 
body: (...), bodyUsed: false, headers: Headers {}, ok: true, redirected: false, status: 200, statusText: "OK", type: "basic", url: "http://localhost:4000/patient/add"
//client side post 

 handleSubmit(e) {
        e.preventDefault();
        const postUrl = '/patient/add';
        fetch(postUrl, {
            method: 'POST',
            headers: {'Content-Type': 'text/plain'},
            body: this.state.patientName
        })
            .then(response=> {
                if (!response.ok) console.log('failed', response);
                else console.log(response);
            });
    }
  this.app.post('/patient/add', bodyParser.text(),
            this.route_post_patient_add.bind(this));
 async route_post_patient_add(req, res) {
        /** @type {string} */
        const body = req.body;

        if (body === undefined) {
            logger.warning('Set room patient failed, body missing');
            res.sendStatus(400);
            return;
        }

        if (body === "") {
            logger.warning(' body is empty');
            res.sendStatus(400);
            return;
        }

        try { 
            const patient_id = await this.save_patient(body);

            res.send(patient_id);
            console.log(patient_id); //logs the id that is generated

        }
        catch (err) {
            logger.error('Set patient failed, internal error', { err });
            res.sendStatus(500);
        }
    }
1
  • What does the actual response body look like in your browser "Network" developer tool? Commented Sep 9, 2019 at 14:12

1 Answer 1

3

The response object in fetch is not the raw body.

You have to call a function and resolve a promise to get the data.

For example:

fetch("foo")
    .then(parse_body)
    .then(log_data);

function parse_body(response) {
    return response.text();
}

function log_data(response_text) {
    console.log(response_text);
}

Further reading: MDN: Using Fetch

Sign up to request clarification or add additional context in comments.

3 Comments

Not sure how I missed that being as I use this every day wow. I've never used response.text() I guess this is useful for simple strings?
how does the response_text parameter work. whats being passed? I also need to be able to save the id in a var and the available on the handleSubmit @Quentin
@Jereme — The result of resolving the promise returned by the response.text() method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.