In my current project with Angular 16, I am executing a query on the database and assigning the returned number to a document number.
Data Model
export class Document {
doc_data: string ="";
doc_for: string="";
doc_number: number = 0;
doc_year: number = 0;
doc_subject: string = "";
doc_send: number = 0;
doc_employee: number = 0;
}
Service File
number_doc(year: number): any {
// return the value
return this.http.get(`${this.url + 'numberdoc'}/${year}`).pipe(
map((res) => {
let resInt = +res;
return resInt;
})
);
}
App Component File
export class AppComponent {
...
doc = new Document();
...
find_number_doc(year: number): number {
return this.service.number_doc(year).subscribe((res: number) => {
//this.number_doc = res;
this.doc_number = res;
return res;
});
}
ngOnInit() {
//initialization of some properties. I put it more as the shape you are assigning
this.year = formatDate(new Date(), 'yyyy');
this.doc.doc_data = formatDate(new Date(), 'dd/MM/yyyy');
this.doc.doc_year = new Date(), 'yyyy';
}
onClick() {
...
this.find_number_doc(this.year);
this.document.download(this.doc);
}
The issue lies in retrieving the database number and assigning it to the property. The first call to "this.doc_number" passes zero to the child component. Upon a second call, it maintains the previous number instead of updating.
Object -> 0
document.service.ts:47 ---- Object -----
document.service.ts:48 {"doc_data":"02/11/2023","doc_for":"","doc_number":0,"doc_year":2023,"doc_subject":"","doc_send":0,"doc_employee":0}
document.service.ts:179 Blob {size: 8593, type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'}
document.service.ts:181 Document created successfully
app.component: 29
Although the number 29 should have been passed, zero was erroneously passed.
Further Investigation
Object -> 29
document.service.ts:47 ---- Object -----
document.service.ts:48 {"docdata":"02/11/2023","doc_for":"","doc_number":29,"doc_year":2023,"doc_subject":"","doc_send":0,"doc_employee":0}
document.service.ts:179 Blob {size: 8594, type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'}
document.service.ts:181 Document created successfully
app.component.ts:131 app.component: 30
Even though the expected outcome was passing the number 30, it passed 29 instead, causing confusion and complexity when attempting to rectify.
30
document.service.ts:47 ---- Object -----
document.service.ts:48 {"doc_data":"02/11/2023","doc_for":"","doc_number":30,"doc_year":2023,"doc_subject":"","doc_send":0,"doc_employee":0}
document.service.ts:179 Blob {size: 8594, type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'}
document.service.ts:181 Document created successfully
app.component.ts:131 app.component: 31
In this scenario, the intention was to pass the number 31, but 30 was inadvertently passed.
I am striving to efficiently transfer the database value to generate a word document. Despite my efforts to troubleshoot prior to reaching out for help, the resolution remains elusive.