0

I have this snippet:

usersTempList=[];

ngOnInit() {
  this.obj = {user: JSON.parse(localStorage.userDetails),room:"Sala" + this.projectID, projectID: this.projectID};
  this.loggedUser = JSON.parse(localStorage.userDetails).UserID;
  this.roomList.push(this.obj.room);
  this.usersTempList=[];
  console.log("Initialized array")
  this.join(this.obj);
  ...
}

join(obj: any){
    this.socket.emit("join",obj,this.getOnline);
}

getOnline(err,msg){
  console.log(this.usersTempList);
  console.log("Printed array");
  this.usersTempList = JSON.parse(JSON.stringify(msg));
  console.log("qwerty",this.usersTempList);
}

Opening this component on the browser, neither *ngFor works (it isn't printing anything) and, the console.log of the first line of getOnline always prints undefined. Also, even that the variable is updated in the subsequent lines, *ngFor still doesn't update the interface.

Does anyone have any clue about this?

Thanks!

2
  • 3
    Please share your HTML code, and first of all here, getOnline is a function which is not returning anything. Commented Mar 14, 2019 at 10:39
  • could you also further specify the output you would want to see? Commented Mar 14, 2019 at 10:42

1 Answer 1

1

The problem was solved, it happened because I couldn't access my array inside my scope, so I used an arrow function and it worked, usersTempList wasn't undefined anymore..

join(obj: any){
  this.socket.emit("join",obj, (err,msg) => {      
        this.usersTempList=JSON.parse(JSON.stringify(msg))
    });
}

Thank you all!

Sign up to request clarification or add additional context in comments.

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.