-1

I am using below code and when i am calling onMessageRender() function, so it says "this is not a function"

request = new Atmosphere.AtmosphereRequest(); // Global Function

Calling below code in ngONinIt

this.createSSE();

//Main function 
 createSSE(){ this.request.onMessage = function (response) {
        console.log('request.onMessage trigger');
         try {
           let jsonData = JSON.parse(message);
          console.log('Normal Message JSON this.request.util', jsonData);  
          this.onMessageRender();    
        }catch (e) {
           console.log('Error in websocket onmessage: ', e);
           return;
         } }

Created below function globally

onMessageRender(){

}

Please let me know if you need more information. Thanks!

Ambuj Khanna
  • 1,131
  • 3
  • 12
  • 32

2 Answers2

1

I would like to answer my own Question because after lots of fight. I got my working solution.

I need to convert my Normal/Old style function into Fat Arrow function because Arrow function always has this reference and it is available for that class object.

Old (Not Working)

this.request.onMessage = function (response) {...}

New (Working)

this.request.onMessage = (response) => {...}

After above changes, now it is accepting all other functions in it.

Thank you all for your time and support!

Ambuj Khanna
  • 1,131
  • 3
  • 12
  • 32
0

Assuming this is an Angular/typescript question,

if createSSE is declared as part of an Angular module, and if onMessageRender() is a global function which is not declared as part of an Angular module, calling onMessageRender() should be without this, as onMessageRender() may not be part of the same object that createSSE is declared.

If onMessageRender() is indeed part of the same object, I think more detail is required to answer your question.

kairun
  • 56
  • 5
  • Ok. Let me try to describe it. **I am using AtmosphereJS for Websockets in my project. `createSSE` is my function in a component in which i am using `this.request.onmessage` here `this.request` is _AtmosphereRequest_ reference object and it contacts Atmosphere.js which contains `onmessage` function and it calls then it come back to my component and execute further code. Outside `createSSE` I have create another function `onMessageRender()` and I want to call it in `this.request.onMessage` function **. I hope I am able to make you understand it. – Ambuj Khanna May 30 '19 at 05:23
  • Hmm okies, can you just call your `onMessageRender()` without `this` keyword @AmbujKhanna? – kairun May 31 '19 at 06:11
  • Try finding out why engine couldn't find the function then, e.g. is your global function available in this context? If it is not, try to find out why it is not available. e.g. if the global function is in the same file, if it's not did you export/import the function correctly? if you did export/import correctly(or if your function is ambient function), did you consider the timing of function being declared and being available in your context? if this is typing issue, did you declare your function with proper typings and etc? – kairun Jun 11 '19 at 05:23
  • with information given so far only answer I can give is limited to generic answers, if these options didn't work then I recommend you to create a public/mock project that replicates this issue and then edit your question with this additional info – kairun Jun 11 '19 at 05:24
  • I got my solution. I just converted my normal function into fat arrow function. `this.request.onMessage = (response) => {.....}` – Ambuj Khanna Jun 12 '19 at 08:09
  • It is declared in same class and it is global function. There is no context or typing mistake. Thankyou! – Ambuj Khanna Jun 12 '19 at 08:17
  • Well if changing to arrow function made this to work then this is a context issue, as normal function may run in different context from time to time. You don't say this is declared in your class, but it just sounds this is declared as a global function only in the context of your file, which doesn't really make it as global function as in globally available function (you won't be able to use this function from different file as ambient function) – kairun Jun 13 '19 at 10:17