skip to Main Content

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;
  }

2

Answers


  1. What is the setValue function? JavaScript is as confused as I am. What are you trying to do? String prototypes have no member called setValue, could you be calling the function on the wrong object?

    edit: I was wrong about removeSpecialCharacters, replace is fine there

    Login or Signup to reply.
  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search