TL;DR(short answer):
your method salvarImagem
doesn’t return anything(explicit salvarImagem(): void
) and looking on your code you should return an object with the property datahoje
Longer answer:
To avoid error you either need to return value
salvarImagem(): { datahoje: Date } {
...
return { datahoje };
}
or call the method and take value from an object which you have edited(as a side effect)
this.selecionado['dataInicial']
A bit about Typescripts’ types:
https://www.typescriptlang.org/docs/handbook/basic-types.html
Also, your IDE “knows” return type – you can check it by ctrl(cmd for MacOS) + hovering over the method.
Well, it happens that I have some code made with angular bootstrap which is a data picker, which is inside an event party register(which you click new and make a new event) and I want the user to choose a date and save it, here goes the code of the date picker:
<p>Selecione a data</p>
<ngb-datepicker #dp [startDate]="modelAtual" [(ngModel)]="modelAtual" (navigate)="date = $event.next"></ngb-datepicker>
<pre>Month: {{ date.month }}.{{ date.year }}</pre>
<pre>Model: {{ model | json }}</pre>
and then I have the code to save the info of the date, which is in typescript:
salvarImagem() {
if (this.chave === undefined) {
let dat1=new Date().getMilliseconds();
const filePath = `/eventos/${dat1}-${this.selecionado["lastModified"]}` + '.' + this.selecionado['tipoarquivo'];
const fileRef = this.storage.ref(filePath);
const task = this.storage.upload(filePath, this.file);
let datahoje = new Date(this.modelAtual.year,this.modelAtual.month,this.modelAtual.day).getTime();
this.selecionado['dataInicial']=datahoje;
var horario = this.time.hour + ':'+ this.time.minute;
this.selecionado['horaInicial']=horario;
// observe percentage changes
this.uploadPercent = task.percentageChanges();
// get notified when the download URL is available
task.snapshotChanges().pipe(
finalize(() => {
this.downloadURL = fileRef.getDownloadURL();
this.downloadURL.toPromise().then(du => {
this.selecionado["arquivo"] = du;
this.service.add(this.selecionado);
this.router.navigate(['/eventos']);
alert("Objeto salvo");
});
})
).subscribe();
} else {
this.service.update(this.chave, this.selecionado);
this.router.navigate(['/estabelecimentos']);
alert("Objeto salvo");
}
}
and then I have the code that loads whenever the user wants to edit this register (which you can do) and in order to show exactly the date he chooses before, I try to assign the variable modelAtual to the variable datahoje, that for some reason returns the error mentioned in the title
if (this.chave !== undefined) {
this.service.get(this.chave).valueChanges().subscribe(obj => {
this.selecionado = obj;
this.logo = this.selecionado["arquivo"];
//this.modelAtual = { year: 2011, month: 7, day:23};
this.modelAtual = this.salvarImagem().datahoje;
})
}
would anyone be able to tell me the cause of that?