Node.js TypeError: Cannot read property '1' of undefined

Node.js TypeError: Cannot read property '1' of undefined

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',
  ]
};

我想创建与 altoken.js 中的模块一样多的频道,但出现错误。 类型错误:无法读取未定义的 属性“1”

您想通过动态 属性 名称访问 属性。您需要括号表示法:

o['opts' + i]

示例:

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})
}