0

index.js

const o = require('./altoken')

    for(const i=1; i<99; i++){
        
        if(o.opts[i].identity.username === null){break;}; //error here
        
        message.guild.channels.create(o.opts[i].identity.username, {type: "text",parent: id})
    }

altoken.js

 module.exports.opts1 = {
  mro: {
    as: false,
  },
  identity: {
    username: "x",
    password: "x"
  },
  krlo: [
    'x',
  ]
};

I want to create as many channels as modules in altoken.js but I am getting an error. TypeError: Cannot read property '1' of undefined

warmesty
  • 25
  • 2
  • 7

1 Answers1

1

You want to access a property by dynamic property name. You need bracket notation:

o['opts' + i]

Example:

const o = require('./altoken')

for(const i=1; i<99; i++){
    
    if(o['opts' + i].identity.username === null){break;}; //error here
    
    message.guild.channels.create(o['opts' + i].identity.username, {type: "text",parent: id})
}
jabaa
  • 5,844
  • 3
  • 9
  • 30