Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

274
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!