The line: sseTopicString.setValue(sseValueNumber); is throwing a Type error: Uncaught TypeError: sseTopicString.setValue is not a function.
If I write out the value (string) of sseTopicString before the . everything works as expected.
What am I not seeing? I tried async / await as well, not expecting much of it. I just don't see why using the variable would make such a difference. Pls help.
All the conversions to strings and numbers are experimental - none of them made a difference.
let sse = new EventSource("http://localhost:3000/sse-stream");
// sse.onmessage = console.log
sse.onmessage = function (event) {
let jdata = JSON.parse(event.data);
console.log(jdata);
let rawSseTopic = jdata.topicname;
let sseTopic = removeSpecialCharacters(rawSseTopic);
let sseTopicString = sseTopic.toString()
console.log(sseTopicString);
let sseValue = jdata.message;
let sseValueNumber = Number(sseValue);
console.log(sseValueNumber);
sseTopicString.setValue(sseValueNumber);
// sseTopicString has the assigned string value "Gauge12"
// changing the line to: Gauge12.setValue(sseValueNumber); works (Gauge changes to value)
// sseTopicString.setValue(sseValueNumber); throws the error
// how can I use te variable sseTopicString to achieve the desired effect?
};
function removeSpecialCharacters(string) {
let cleanString = string.replace(/,|\.|-|_|\|\s|\//g, "");
return cleanString;
}
Edit: After your feedback (thanks!!) and some thinking, the question was meant to be:
I have an instance of a class "Guage" running at the time of Server Sent Event.
The instance is a gauge/ a pointer. I can adjust/update the pointer by static code:
Gauge12.setValue(sseValueNumber); or from Backend / EJS: <%= gaugeInstanceVar %>.setValue(<%= gaugeValueVar %>);
At the time my variable sseTopic or sseTopicString, containing the value "Gauge12" comes sent in by the server, the instance Gauge12 is already running.
How can I use the variable to mimic Gauge12.setValue(sseValueNumber);.... because sseTopicString.setValue(sseValueNumber); seems to just modify my variable, instead of modifying the Gauge instance.
Edit 2:
Using eval() would also achieve the desired effect
eval(sseTopic).setValue(sseValueNumber);
But I believe I should stay away from it and am still looking for the correct way of achieving the equivalent.