0

For register user const response= await axios.post() But for login user Const {data} = await axios.post()

What is the need of {data}?

3 Answers3

1

It extract the key data from the object that axios.post() returns. https://dmitripavlutin.com/javascript-object-destructuring/

mgm793
  • 1,968
  • 15
  • 23
0

This is called Destructuring assignment

If you look into the axios response schema, you will see it has a key by name data which contains the response from the server. So destructuring the axios response will take the data from the axios response and will assign to data variable const {data} = await axios.post()

For example if you do const {data,status} = await axios.post() it will assign the status from the axios response into the status variable.

{
  // `data` is the response that was provided by the server
  data: {},

  // `status` is the HTTP status code from the server response
  status: 200,

  // `statusText` is the HTTP status message from the server response
  // As of HTTP/2 status text is blank or unsupported.
  // (HTTP/2 RFC: https://www.rfc-editor.org/rfc/rfc7540#section-8.1.2.4)
  statusText: 'OK',

  // `headers` the HTTP headers that the server responded with
  // All header names are lower cased and can be accessed using the bracket notation.
  // Example: `response.headers['content-type']`
  headers: {},

  // `config` is the config that was provided to `axios` for the request
  config: {},

  // `request` is the request that generated this response
  // It is the last ClientRequest instance in node.js (in redirects)
  // and an XMLHttpRequest instance in the browser
  request: {}
}
brk
  • 48,835
  • 10
  • 56
  • 78
0

This is "object destructuring".

You just get the object property called "data".

When you call axios, there is an object returned, and instead of doing

const login = axios.post()
const data = login.data

You're directly accessing the data property.

Le D
  • 115
  • 7