0

I have a Angular project and I use the Signalr inside it. But when I get the connectionId , I can print it. But I can't assign it in the global variable. This is my code:

   connection = new signalR.HubConnectionBuilder().withUrl("http://localhost:8178/notifyHub").build();
    connection.start().then(function () {
      connection.invoke("GetConnectionId").then(function (connectionId) {
       console.log(connectionId);
       // var temp=connectionId
      })
    }).catch(err => console.error(err));
4
  • declare outside typescript class var temp="" or inside typescript constructor var temp:string = ""; and then assign inside typescript class. Commented Oct 14, 2019 at 5:36
  • 1
    @jitender exactly Commented Oct 14, 2019 at 5:38
  • The problem is the scope of your function. As per @jitender's answer, fat arrow notation will allow the use of the correct scope, unless you implicitly want block level scope. Commented Oct 14, 2019 at 6:05
  • @mohammadalmasi accepted ans is not the right way to solve the problem Commented Oct 14, 2019 at 6:16

3 Answers 3

3

You can solve the same using the following :

temp;   
let self = this;
let connection = new signalR.HubConnectionBuilder().withUrl("http://localhost:8178/notifyHub").build();
connection.start().then(function () {
  connection.invoke("GetConnectionId").then(function (connectionId) {
    console.log(connectionId);
    self.temp = connectionId;
  })
}).catch(err => console.error(err));

or the better solution is to use use arrow function expression :

temp;
connection = new signalR.HubConnectionBuilder().withUrl("http://localhost:8178/notifyHub").build();
connection.start().then(() => {
  connection.invoke("GetConnectionId").then((connectionId) => {
    console.log(connectionId);
    this.temp = connectionId;
  })
}).catch(err => console.error(err)
Sign up to request clarification or add additional context in comments.

4 Comments

any reason behind not using arrow function and performing extra steps to active the same thing?
@jitender you are right using arrow functions is better , i have nothing against it though :)
you can update your answer to use arrow function so later if someone else come here with same problem then OP should goes to right direction
@jitender i have updated the answer as you requested
2

instead of function use arrow function expressions like

connection = new 

   signalR.HubConnectionBuilder().withUrl("http://localhost:8178/notifyHub").build();
    connection.start().then(()=> {
      connection.invoke("GetConnectionId").then((connectionId)=> {
       console.log(connectionId);
       // here you will be able to access this
      })
    }).catch(err => console.error(err));

2 Comments

@jitender Why this way the first value is always undefined??
first value means?
0

Try this way:

connectionId: any;

connection = new 
   signalR.HubConnectionBuilder().withUrl("http://localhost:8178/notifyHub").build();
   connection.start().then(function () {
   connection.invoke("GetConnectionId").then(function (connectionId) {
   console.log(connectionId);

    this.connectionId = connectionId 

  })
}).catch(err => console.error(err));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.