Good afternoon, I'm having to make the typescript excute a call every 10 milliseconds.
I have a search using setInterval but the information comes as undefined but when I save it, the values in this.tags.name
come as undefined.
export class InicialComponent implements OnInit {
tagsMongoUrl = 'http://localhost:8080/tags';
tags = [];
tagsSave = new Tags();
msgs: Message[] = [];
nome = '';
valor = '';
constructor(private principalService: PrincipalService, private http: Http) { }
ngOnInit() {
this.pesquisar();
}
pesquisar() {
this.principalService.pesquisar()
.then(() => null);
}
salvar() {
setInterval(function () {
const v = 100;
for (let i = 0; i < v; i++) {
this.tagsSave.name = 'name ' + i;//undefined aqui pois não acessa o objeto
this.tagsSave.value = i.toString();
this.nome = this.tagsSave.name;
this.valor = this.tagsSave.value;
console.log(this.valor);
this.saveData();
}
}, 3000);
}
saveData() {
const headers = new Headers();
headers.append('Content-Type', 'application/json');
return this.http.post(this.tagsMongoUrl,
JSON.stringify(this.tagsSave), { headers })
.toPromise()
.then(response => response.json());
}
}