node.script - how to get dynamic value from message object AFTER it is required to run the js??

mradentz's icon

I'd like to enter a UNIX timestamp dynamically from a message object (or list, coll, etc.) to populate an api call using the patcher & js below:

Max Patch
Copy patch and select New From Clipboard in Max.

history_003_coincap.io.js
js 2.25 KB
.js file for above patcher

Total js noob. Any help greatly appreciated.

Thanks in advance.

mradentz's icon

bump.

Even a link to Js for Dummies would help. I've done the tutorials & documentation and I know its right in front of me but...

I'll keep punching away but any hints would really help. No fish. No barrel. No gun. :-(

Again, thanks in advance

mradentz's icon

Still on it but making little progress:

autowatch = 1

const maxApi = require('max-api');

//do I actually to need declare "inlets" & "outlets" for this script?
inlets = 1;
outlets = 2;

var https = require('https');

//using a constant the js works great:
//
//var endTime = 1509213952000;

//let's try Max.addHandler...

maxApi.addHandler("endTime", async () => {
    
    await endTime;
    maxApi.outlet(1, endTime);
        
});


var endTime = (endTime);

//message object "endTime 1509213992000" passes int value to maxApi.outlet(1, endTime) in the Handler but "var endTime = (endTime)" does not get the value.  why?


//60 seconds prior to var "endTime", i.e. get a one minute range of data.
var start = (endTime - 60000);


//coincap.io provided api call code

var options = {
  'method': 'GET',
  'hostname': 'api.coincap.io',
  
//GET api url created for variable range of requested data using var "start" and //var "endTime".

  'path': '/v2/assets/bitcoin/history?interval=m1&start='+(start)+'&end='+(endTime)+'',
  
  'headers': {
  }
};

//etc

This seems such a simple process on the surface but I just don't get how to make it work.

I'll keep poking at it.

Till next time...

mradentz's icon
mradentz's icon

looking at callbacks.

mradentz's icon

nobody?

geofholbrook's icon

maxAPI.addHandler is the correct way to respond to a message coming in. But in your attempt, there is an odd thing: you say "await endTime". (Insert apocalypse joke here.) endTime is just a known value, it's not asynchronous.

One thing is for sure: the code you have in the global scope is only going to get called once. I think what you want to do is run your http request when an endTime message is provided, is that right? In that case the handler function would indeed be async, but it would await the request call. All that code you have in the global scope needs to go inside the handler. Or, to be cleaner, put it inside an async function called 'fetchData' or something, and call that from the handler.

mradentz's icon

GEOFHOLBROOK,

Thanks so much for pointing me in a productive direction!! So much to learn.
I'd almost given up on this. Back to the js tutorials!

"I think what you want to do is run your http request when an endTime message is provided, is that right?"
Exactly! Thanks again.

"(Insert apocalypse joke here.)"
That's GOLD Jerry! :-)