0

const sendData = result => {
    result.centers.forEach(center => {
        const data = {
            center_id,
            name,
            address,
            district_name,
            state_name,
            pincode,
            fee_type,
            sessions
        } = center ;
        data.sessions = data.sessions.map(session => {
            return {
                date,
                available_capacity,
                min_age_limit,
                vaccine
            } = session ;
        })
        console.log(data) ;
    })
}

why does the above code produce the following error ?

PS: Please ignore the line numbers.

UnhandledPromiseRejectionWarning: TypeError: Assignment to constant variable.
    at /home/ubuntu/Projects/Web/CowinTracker/server/app.js:69:17
    at Array.map (<anonymous>)
    at /home/ubuntu/Projects/Web/CowinTracker/server/app.js:67:39
    at Array.forEach (<anonymous>)
    at sendData (/home/ubuntu/Projects/Web/CowinTracker/server/app.js:56:20)

I tried changing const data to let data and same error occurred.

me.nkr
  • 108
  • 1
  • 7
  • will the problem solve when you change const to let? or still receiving this error then? – Hadi R. May 08 '21 at 17:02
  • @HadiR. yes changing to let produces same error – me.nkr May 08 '21 at 17:04
  • 1
    What are you trying to do with `return { ... } = data;` ? – Jonas Wilms May 08 '21 at 17:12
  • @JonasWilms replace data.sessions with the returned array. – me.nkr May 08 '21 at 17:15
  • @chiliNUT that is using destructuring to take only needed key value pairs from session object, which is an element of data.sessions array – me.nkr May 08 '21 at 17:19
  • 1
    `const data = { center_id, … } = center;` assigns `center` to `data` and creates a lot of [implicitly global variables](http://blog.niftysnippets.org/2008/03/horror-of-implicit-globals.html). Whatever you meant to do there, this is not what you want. See also https://stackoverflow.com/a/44531789/1048572 – Bergi May 08 '21 at 17:28
  • @Bergi I see all those variables in global scope and it changes values with each iteration. Is there a better way to do this ? – me.nkr May 08 '21 at 17:54
  • 1
    @me.nkr It's not even clear *what* you're trying to do there, so I can't tell. – Bergi May 08 '21 at 18:27

1 Answers1

1

You can't destructure in your return statement.

You need to either destructure the session argument, or reference the session object and assign the properties to a new object.

const data = {
  centers: [
    {
      center_id: 1,
      name: 'One',
      address: '1 one street',
      district_name: 'one',
      state_name: 'one',
      pincode: '1111',
      fee_type: 1,
      sessions: [
        {
          date: new Date(),
          available_capacity: 1,
          min_age_limit: 1,
          vaccine: "its gene therapy"
        }
      ]
    }
  ]
}


const sendData = result => {
    result.centers.forEach(center => {
        const data = {
            center_id,
            name,
            address,
            district_name,
            state_name,
            pincode,
            fee_type,
            sessions
        } = center ;
        data.sessions = data.sessions.map(session => {
            return {
                date: session.date,
                available_capacity: session.available_capacity,
                min_age_limit: session.min_age_limit,
                vaccine: session.vaccine
            } ;
        })
        console.log(data) ;
    })
}

sendData(data)

If you wanted to destructure the session argument you could do something like this.

data.sessions = data.sessions.map((( 
  date, 
  available_capacity,
  min_age_limit,
  vaccine,
}) => ({
    date,
    available_capacity,
    min_age_limit,
    vaccine,
}) 

})

If you're just cloning the object you could do the following.

data.sessions = data.sessions.map(session => ({
  ...session
}))
synthet1c
  • 6,152
  • 2
  • 24
  • 39
  • so can't I do a destructuring, assaign it to a variable and return that variable ? I tried that and got the same error. why is that ? – me.nkr May 08 '21 at 17:28
  • 1
    Destructuring save variables to the current scope, its a short hand way to assign properties to variables of the same name. it works for `center` because you are saving variables inside the forEach loop scope. As said you could destructure the session argument, but it wouldn't save you any characters. `data.session.map(({ date,available_capacity }) => ({ date, available_capacity })`. It looks like you are just returning the session object without doing anything, there are better ways to shallow clone the object and return it. – synthet1c May 08 '21 at 17:35
  • Your code worked. I'm not cloning just taking needed values. – me.nkr May 08 '21 at 17:45