0

I im using node js to pass data from watcher every few seconds to function streamplay that stores id.ts filenames in array this works fine as it stores but when i callback from another function in my program i get undefined value from this array....and i can't figure out how it needs to be written:

var stream_play = [];

watch('/tmp/1_.m3u8', function(event, name) {
    /* DEFINE - variables */
    var ts    = [];
    ts.push(1);

    streamplay(id, ts[ts.length-1]);
});

function streamplay(id, ts, callback) {
   /* CHECH - ts */
   if (ts !== 0) {
      console.log('adding id..'+id+'...with ts...'+ts)
      stream_play[id] = ts;
   } else {
      return callback(stream_play[id]);
   }
}

And calling from main function in node like this:

 streamplay(stream, 0, function(response) {
       console.log('streaming ts file...'+response+'...to client...')                   
       res.write(fs.readFileSync('/tmp/'+response));
  })

I get this:

streaming ts file...undefined...to client...
1
  • Because stream_play array is empty Commented Sep 27, 2017 at 13:14

2 Answers 2

3

Your function call, streamplay(stream, 0, function(response) { will end up falling through to your else condition in streamplay. This line is return callback(stream_play[id]); and the stream_play array is initialized, but empty. Accessing a non-existent element in a JavaScript array returns undefined.

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

4 Comments

Yes i know that is empty but how to make array globally to make it to return value...i put variable outside function to make it globally but array is still empty
One option would be to define stream_play like this: var stream_play = [0];
I added var stream_play = [0]; globally but it is sam undefined
No one have idea what it needs to done? Maybe using buffers? I read on node that buffer is global and is accessed throught whole node js application...i im using using ts fs.readfilesync...so maybe this can be done globally?
0

I found the solution and another problem...the first example using callback works now like charm. The problem was that i have master process and worker process and i read on node js site that master and worker process can't share between variables values...so the new question is how to share master and worker process variables so that i can access from all worker process variables values from master?

I im doing clustering with this because of my huge server load and cpu clustering is must to go.

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.