0

This seemed to be easy one but after of couple solutions I gave up not knowing Node.js very well.

So the question is how to return a value from that function (this code below won't work):

let content;
app.post('/problems', function(req, resp){
  resp.append('Access-Control-Allow-Origin', '*')
  resp.append('Access-Control-Allow-Headers', 'Content-Type')
  console.log(req.body)
  resp.json(req.body)
  content = req.body;
  return content
})

console.log('content' + content)

As I understand it is a promise but syntax won't work either:

.then(() => console.log(content))

Thank you for helping.

Murakami
  • 3,474
  • 7
  • 35
  • 89
  • This looks like you’re defining a route, not making a request. It could be called multiple times, so returning a promise from `app.post()` doesn’t make sense. What are you trying to do? – Ry- Mar 27 '18 at 14:08
  • Why? That makes no sense at all – Jonas Wilms Mar 27 '18 at 14:09
  • I think OP wants to store the request in the content variable for later use... @Ryan – Michael Curry Mar 27 '18 at 14:11
  • @JonasW. I can't say why, but in the route definition it looks like the OP is forgetting that it's async and will only be called when the route is activated. OP, if you want to use the req, you have to do it in the route definition. – Michael Curry Mar 27 '18 at 14:17
  • All I wanna do here is just to save incoming value (it comes from react.js form) and to send it via email (using nodemailer). So the value needs to be passed to nodemailer function below this code. – Murakami Mar 27 '18 at 15:22

0 Answers0