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

281
Visualizações
Arrays and objects are empty in console.log {sometimes } and when sending over socket.io

When I send my arrays or objects over socket.io with socket.emit or anything I get an empty array or object {} or []

I have an array named metadata generated with a p5js script and when I console.log(metadata) it shows up as it should in console.log, I can also see the length array in chromes console.log

but when I go look at the individual sections of the array with console.log(metadata[1]) I get undefined... the same thing occurs when I try to read the length, it returns 0 even though as you can see in the image it does indeed populate.

JSON.stringify() also returns an empty array or object

here is my client ejs code

<script >
    let metadata= new Array();
     
    const Helper = {seed: <%=d%> , setMetadata: function  (mn,bm)  { metadata.push(mn); metadata.push(bm) }};
    
         console.log(metadata)
         console.log(metadata[1])

    const socket = io('http://localhost:3002');

   socket.on('connection')

   socket.emit("message",  metadata);
  
</script>

and here is my socket.io code on the server, It does indeed communicate when I send strings and when a user connects. The console also displays JSON and objects correctly.

var socketIO = require("socket.io") (http, {
  cors: {
    origin: "*"
  }
});

http.listen(3002, function () {
  console.log("server started....");
});
  socketIO.on("connection", function (socket) {
      console.log("user connected: "+ socket.id)  

 socket.on("message",  (data) => {
          let pqa = data
          console.log(pqa);
    });

   });


let zzz = {"oh": "what is this "}
let ccc = JSON.stringify(zzz)
console.log(ccc)

I have also tried setting metadata into an object {}, reformating how I pushed it into the object and array with the => operator and I get the same problem every time.

EDIT:

So as someone suggested I tried console logging mn and I got an error mn not defined when I visited it with my chrome browser.. After that I removed that console log and tried accessing the site with a headless browser specifically the npm package puppeteer, and the entire array went through the socket and it works!... I am so lost??? why does it work for my headless browser and not my chrome browser?? btw the headless browser is also chromium based lol..

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