1

I'm working with socket.io. I am having trouble receiving data from the server even though I can console.log() the data (an array of objects) right before I try to emit the data back to the calling client. If I hard code info into the emit, it will display on the client but when I use the dynamically created array of objects, it doesn't go through. My gut tells me its a asyn issue but I'm using bluebird to manage that. Perhaps its an encoding issue? I tried JSON.stringify and JSON.parse, no dice.

Server

 socket.on('getClassList', function(){
    sCon.getClassList()
    .then(function(data){
        console.log(data) //data is an array full of objects
        socket.emit('STC_gotDatList', data)
    })    
  })

Hard-Coded Expected Output:

classes['moof'] = {
    accessList: [888],
    connectedList: [],
    firstName: "sallyburnsjellyworth"
}

Client

socket.on('STC_gotDatList', function(info){
    console.log(info) //prints [] or {}
})

EDIT:

I remember reading somewhere that console.log() may not be printing data at the time the data is available/populated. Could that be it even though im using Promises? At the time I'm emitting the data, it just hasn't been populated into the array? How would i troubleshoot this scenario?

EDIT2:

A step closer. In order to get anything to return, for some reason I have to return each specific object in the 'classes' array. It wont allow me to send the entire array, for the example I gave above, to get data to the client, I have to return(classes['moof']), can't return(classes) to get the entire array... Not sure why.

EDIT3: Solution: You just can't do it this way. I had to put 'moof' inside classes as a property (className) and then I was able to pass the whole classes array.

1 Answer 1

1

How is the 'classes' array created?

The problem might be that it is being given properties dynamically, but has not been created as an object, but rather an array. If you plan to dynamically add properties to it (like property moof), you should create it as an object (with {}) rather than an array (with []).

Try

var classes = {};//instead of classes = []
//then fill it however you do it
classes[property] = {
    accessList: [888],
    connectedList: [],
    firstName: "sallyburnsjellyworth"
};

Also, the credit goes to Felix, I'm just paraphrasing his answer: https://stackoverflow.com/a/8865468/7573546

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.