Guards won't move I'm probably stupid [javascript]

My issue has to do with the guardAct() function. I made an array to find the nearest monsters, but and classified it so that my guards would not cross onto the other side, but my guards won’t move at all. Please help.

There is also an error when the ai tries to read the x-pos of the enemy.

function guardAct(data) {
    const guard = data.guard;
    const place = data.place;
    while (true) {
        var enemyIndex = 0;
        var enemies = guard.findMonsters();
        while(enemyIndex < enemies.length){
            var enemy = enemies[enemyIndex];
            if (enemy) {
                if(enemy.pos.x < 55){
                    guard.attack(enemy);
                    if(guard.isReady('roar')){
                        guard.special('roar');
                    }else if(guard.isReady('haste')){
                        guard.special('haste');
                    }else{
                        guard.attack(enemy);
                    }
                }else{
                    enemyIndex +=1;
                }
            }
        }
    }
}

hero.on('spawn-guard', guardAct);
function begin(){
    var archerBack = hero.build('archer', "A");
    hero.setTargeting(archerBack, 'strong');
    hero.build('cannon', 'B');
    hero.build('cannon', 'C');
    hero.special('push', 13, 51);
    hero.build('archer', 'H');
    hero.build('archer', 'I');
    hero.special('teleport', 21, 51);
    hero.build('archer', "F");
    hero.build('archer', "G");
    hero.build('mage', 'E');
    hero.build('mage', 'D');
    hero.special('pull', 40, 51);
    hero.build('archer', 'A');
    hero.special('teleport', 38, 51);
}
while(true){
    begin();
}

Monsters don’t have pos property as the first, also with such code you easily can get an infinite loop as if an enemy is dead (easily can be as beween cycles can be several seconds, then you get repeating first if. be careful with while loops and check that you always increment a counter or you have break condition.