Monday, 3 April 2017

ExpressJS doesn't receive the response in a post

I hope that you can help me:

I have an API in my localhost to be the bridge between my front-end and my database.

I am using Express in front-End server. When I submit a form, the data is placed in the database and my API send the response as I expect, but express doesn't receive anything and leaves the status pending (after some minutes my browser reject the response):

Here is my code using express (ClientApi is the instance of my custom API that runs in the localhost):

lisaApp.post('/signup', (req, res) => {
  const validationResult = validateSignupForm(req.body);
  if (!validationResult.success) {
    return res.status(400).json({
      success: false,
      message: validationResult.message,
      errors: validationResult.errors
    });
  }

  var newClient = req.body
  newClient.clientemailname = newClient.userclient
  newClient.client_id = uuid.v4()

  delete newClient.confirmPassword

  ClientApi.saveClient(newClient, function (err, usr) {
    if (err) return res.status(500).send(err.message)

    console.log('entro')
    res.redirect('/')
  });

});

Here is the function of my API, that returns the response as I expect (see that I set my headers with Access-Control-Allow-Origin):

hash.set('POST /', async function saveClient (req, res, params) {
  let client = await json(req)

  if (!db.connected) {
    await db.connect()
  }

  let createdClient = await db.saveClient(client)

  delete createdClient.clientemailname
  delete createdClient.password

  await db.disconnect()
  res.setHeader('Access-Control-Allow-Origin', '*')
  send(res, 201, createdClient)
})

And after send the data through a form, here is the error that my browser shows me:

Failed to load resource: net::ERR_EMPTY_RESPONSE
Uncaught (in promise) TypeError: Cannot read property 'data' of undefined
    at app.js:18952



via maoooricio

No comments:

Post a Comment