简体   繁体   中英

Getting response body from a NodeJS Express POST response

I'm running into an issue with NodeJS and Express which seems like it should be "Hello World" simple, but has been stumping me all day. Here's my NodeJS POST code:

app.post('/evaluateScore', (req, res) => {
  output = calculators
    .evaluateScore({
      handicap: req.body.handicap,
      score: req.body.score,
      rating: req.body.rating,
      slope: req.body.slope,
      holes: req.body.holes
    })
   console.log(output)
   res.mydata = {}
   res.mydata.output = output
   console.log("res.mydata.output: " + res.mydata.output)
   res.end()
})

And here's the client code that calls it:

post('/evaluateScore', { handicap, score, rating, slope, holes })
    .then(({ mydata }) => {
        console.log(mydata)
        Evaluator.querySelector('.result').value = mydata.output
        })

My problem is that no matter what I've tried to do with my {output} value, which is generated correctly in the POST server-side, I cannot send it back successfully to the client. I've tried all kinds of things:

res.json
res.send
res.data

I've found threads claiming that each of those work in some way, but the only thing that the client ever seems to have access to is the 'status', which I can either send with a res.sendStatus(XXX) call, or will be 200 by default.

Thanks in advance

Any Errors? What do the console.log() lines reveal?

Does this not work?

app.post('/evaluateScore', (req, res) => {
  output = calculators
    .evaluateScore({
      handicap: req.body.handicap,
      score: req.body.score,
      rating: req.body.rating,
      slope: req.body.slope,
      holes: req.body.holes
    });
   console.log(output);
   res.json(output);
});

Also, maybe this in the caller:

post('/evaluateScore', { handicap, score, rating, slope, holes })
    .then(mydata => {
        console.log(mydata);
        Evaluator.querySelector('.result').value = mydata
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM