Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

104
Visualizações
JavaScript async remove firebase document

I have a function that checks if bullet hits a player, and if so it should remove the bullet and the player that got hit.

I have check_if_bullet_hit_player(bulletRef) function that gets a bullet ref, and the id of the player got shot.

I also have a shoot() function that calls the check_if_bullet_hit_player(bulletRef) for every bullet's movement.

Currently when a bullet hits a player, the bullet disappear, but I can't make the player got hit to disappear also.

Attempt - 1:

I tried the following shoot function:

function shoot(speed=0.5, distance=5, targetX, targetY){
 var shoot = setInterval(function() {
  check_if_bullet_hit_player(bulletRef).then(player_got_shot_id => {
     if (player_got_shot_id != ""){
       clearInterval(shoot); 
       bulletRef.remove();
       hitPlayerRef = firebase.database().ref(`players/${player_got_shot_id}`);
       hitPlayerRef.once("value").then(function(){
         alert("hi");
         hitPlayerRef.remove();
       })
     }
   })
 })
}

I get alert hi when a player gets hit, and the player that gets hit disappear for a moment, then re-created (which is not what is wanted). I think that the player get re-created because of async, so I tried different methods.

Attempt - 2:

I tried as the following stack overflow's answer, which waits for the ref to return:

function shoot(speed=0.5, distance=5, targetX, targetY){
 var shoot = setInterval(function() {
  check_if_bullet_hit_player(bulletRef).then(player_got_shot_id => {
     if (player_got_shot_id != ""){
       clearInterval(shoot); 
       bulletRef.remove();
      await firebase.database().ref(`players/${player_got_shot_id}`).remove();
     }
   })
 })
}

and I get:

Uncaught SyntaxError: await is only valid in async functions and the top level bodies of modules

Attempt - 3:

I tried to wait with then:

function shoot(speed=0.5, distance=5, targetX, targetY){
 var shoot = setInterval(function() {
  check_if_bullet_hit_player(bulletRef).then(player_got_shot_id => {
     if (player_got_shot_id != ""){
       clearInterval(shoot); 
       bulletRef.remove();
       hitPlayerRef = firebase.database().ref(`players/${player_got_shot_id}`);
       return hitPlayerRef.get();  // HERE we chain the promise
     }
   }).then(hola => {
       hola.remove();
    })
 })
}

But nothing happens.

How can I make the player got hit to disappear?

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda