3

I want to create an object with variable key with the help of class instance. I have tried by the following code

class ACL {
    constructor(key){
        this[key] = {
            read: false,
            write: false,
            delete: false
        }
    }

    setRoleReadAccess(key, value){
        [key] = {
            read: value,
        }
    }
    setRoleWriteAccess(key, value){
        [key] = {
            write: value,
        }
    }
    setRoleDeleteAccess(value){
        [key] = {
            delete: value,
        }
    }
}

const userACL = new ACL("1234");
userACL.setRoleReadAccess("1234", true);
userACL.setRoleWriteAccess("1234", true);
console.log(userACL);

But i am getting this error UnhandledPromiseRejectionWarning: TypeError: Invalid attempt to destructure non-iterable instance

i am expecting the following console output

ACL {
 "1234": {
    read: true,
    write: true,
    delete: false
  }
}

Can anyone tell me how can resolve this

4
  • what is [this.key] in the constructor? Commented Jun 7, 2022 at 11:19
  • yes i am passing in constructor now Commented Jun 7, 2022 at 11:23
  • change [this.key] to this[key] and tell me what happens Commented Jun 7, 2022 at 11:31
  • @FranciscoSantorelli getting same error Commented Jun 7, 2022 at 11:39

2 Answers 2

3

[this.key] = and [key] = are trying to do iterable destructuring on what follows the =, but you can't do iterable destructuring on plain objects, they aren't iterable.

class ACL {
    constructor(key) {
        this[key] = { // ***
            read: false,
            write: false,
            delete: false
        }
    }

    setRoleReadAccess(key, value){
        this[key] = {   // ***
            read: value,
        }
    }
    setRoleWriteAccess(key, value){
        this[key] = {   // ***
            write: value,
        }
    }
    setRoleDeleteAccess(key, value){ // ** I added a missing `key` parameter here
        this[key] = {   // ***
            delete: value,
        }
    }
}
Sign up to request clarification or add additional context in comments.

8 Comments

I guess from his comment he 's passing key through the constructor
@AlanOmar - Sigh, yeah, that wasn't there before. I'll update.
now I am passing key in constructor
@AvinashAnshu - Yeah, I've updated.
@T.J.Crowder I tried again but getting same error class ACL { constructor(key){ this[key] = { read: false, write: false, delete: false } } setRoleReadAccess(key, value){ [key] = { read: value, } } setRoleWriteAccess(key, value){ [key] = { write: value, } } setRoleDeleteAccess(value){ [key] = { delete: value, } } }
|
1

When you do [key] = {}, you are trying to do a destructuring, which doesn't work because you can't destructure an object that way. It only works on arrays.

What you need is an assignment to this[key].

class ACL {
  constructor(key) {
      this[key] = { 
          read: false,
          write: false,
          delete: false
      }
  }

  setRoleReadAccess(key, value){
      this[key] = {   
          ...this[key],
          read: value,
      }
  }
  setRoleWriteAccess(key, value){
      this[key] = {   
        ...this[key],  
        write: value,
      }
  }
  setRoleDeleteAccess(key, value){ 
      this[key] = {  
        ...this[key],  
        delete: value,
      }
  }
}

const userACL = new ACL("1234");
userACL.setRoleReadAccess("1234", true);
userACL.setRoleWriteAccess("1234", true);
console.log(userACL);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.