Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

275
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda