1

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.

  • Why [nodejs], [express] and [gauge] tags? – pierpy May 06 '23 at 04:58
  • 1
    You did `sseTopicString = sseTopic.toString()` so I would expect that `sseTopicString` is a _string_. But then you are trying to call `sseTopicString.setValue`. A _string_ has no such function called `.setValue` Perhaps you meant to say `sseTopic.setValue(sseValueNumber)`? – Wyck May 06 '23 at 05:01
  • @pierpy nodejs is serving, the server sent events via express. The JS library used is SVG Guages. As a noob I just wanted to provide as much information as possible – DegreeInNoobology May 06 '23 at 05:13
  • @Wyck let's say sseTopic (as well as sseTopicString) contain a value of 'Gauge12'. When I type out 'Gauge12.setValue(sseValueNumber);' in my code, the Gauge number 12 adjusts to the value in sseValueNumber. I am trying to make the same happen via the variable sseTopicString, containing 'Gauge12'. What is the difference, that one works, but the other does not? – DegreeInNoobology May 06 '23 at 05:21

1 Answers1

0

As pointed out by other comments, the String object has no setValue() method. The correct way to assign a new value is to use the = operator. So, change the line sseTopicString.setValue(sseValueNumber) to:
sseTopicString = sseValueNumber without intermediate conversions, that should be fine. Maybe you are confusing the method setValue() which comes from a library, or other objects.
Docs for String object are here

pierpy
  • 897
  • 3
  • 14
  • 18
  • Thank you for getting back. I believe Gauge12 is already defined in the used library and Gauge12.setValue(sseValueNumber) is already returning the desired results. I am trying to make this dynamic and call Gauge12 (as well as all other gauges) by placing a variable containing the gauge before .setValue. I am not trying to change the value of sseTopicString – DegreeInNoobology May 06 '23 at 05:46
  • Ok, "I believe", but evidently it's not! The error says that.... – pierpy May 06 '23 at 05:55
  • I am not denying the error of my ways :) I am obviously implementing my idea the wrong way, I've found something like an instance of the gauges in the library with the setValue function / method, but I can only fit in a small snippet: instance = { setValue: function(val) { value = normalize(val, min, limit); if(gaugeColor) { setGaugeColor(value, 0) } updateGauge(value); }, – DegreeInNoobology May 06 '23 at 06:07