Stworzyłem aplikację w Vue.js. Dlaczego podczas wykonania funkcji submit w printer-posnet-card.vue model.dto.target jest undefined pomimo ustawienia wartości w ?
import { GenericNamedGuidEntity } from "@/views/commerce/common/named-item";
import PrinterPosnetTarget from "@/views/commerce/printers-posnet/printer-posnet-target";
export default class PrinterPosnetTarget {
name: string;
id: number;
public static instance = {
reception: new PrinterPosnetTarget(0, "Recepcja"),
shop: new PrinterPosnetTarget(1, "Sklep"),
kiosk: new PrinterPosnetTarget(2, "Kiosk")
};
public static readonly All: PrinterPosnetTarget[] = Object.values(PrinterPosnetTarget.instance);
Problem Summary:
You have a Vue.js application where you’re using the <printer-posnet-target-selector> component with v-model="model.target". However, when you execute the submit function, model.dto.target is undefined, even though you’ve set a value through the selector.
Root Cause:
The issue arises due to a type mismatch between what the printer-posnet-target-selector component emits and what the PrinterPosnet model expects for the target property.
Detailed Analysis:
Model Configuration:
In printer-posnet.ts, the PrinterPosnet class defines target as an instance of PrinterPosnetTarget.
The dto getter in the PrinterPosnet class tries to access this.target.id.
Selector Component:
In printer-posnet-target-selector.vue, the <v-autocomplete> component uses item-value="name". This means the v-model receives a string (the name) rather than an id or the entire PrinterPosnetTarget object.
Consequently, model.target is being set to a string (the name) instead of a PrinterPosnetTarget object.
Resulting Issue:
When submit is called, model.value.dto.target attempts to access target.id. But since target is a string (the name), target.id is undefined.
Solution:
To resolve this issue, ensure that the printer-posnet-target-selector component correctly emits the id of the target rather than the name. Here’s how you can achieve this:
Update printer-posnet-target-selector.vue to Use id as Item Value:
Modify the <v-autocomplete> in printer-posnet-target-selector.vue to use id instead of name for item-value. This ensures that the v-model binds to the id (a number) instead of the name (a string).
Adjust the PrinterPosnet Model to Handle target as an ID:
Update the PrinterPosnet class to handle target as a number (the ID) rather than as a PrinterPosnetTarget object. Modify the dto getter to directly use the target ID.
typescript
// printer-posnet.ts
import { GenericNamedGuidEntity } from "@/views/commerce/common/named-item";
import PrinterPosnetTarget from "@/views/commerce/printers-posnet/printer-posnet-target";
export default class PrinterPosnet extends GenericNamedGuidEntity {
printerId: string | null = null;
ipAddress: string | null = null;
targetId: number = 0; <!-- Changed from PrinterPosnetTarget to number -->
organizationUnitId: number = 0;
isActive: boolean = false;
public static fromDto(dto: any): PrinterPosnet {
const entity = new PrinterPosnet();
entity.readDto(dto);
return entity;
}
public static empty(): PrinterPosnet {
const entity = new PrinterPosnet();
return entity;
}
protected readDto(dto: any) {
super.readDto(dto);
this.printerId = dto.printerId;
this.ipAddress = dto.ipAddress;
this.targetId = dto.target ?? 0; <!-- Set targetId from dto -->
this.organizationUnitId = dto.organizationUnitId;
this.isActive = dto.isActive;
}
get displayName() {
return `${this.name} (${this.id})`;
}
get dto(): any {
const baseDto: any = {
name: this.name,
isActive: this.isActive,
printerId: this.printerId,
ipAddress: this.ipAddress,
organizationUnitId: this.organizationUnitId,
target: this.targetId <!-- Use targetId directly -->
};
if (this.id !== null && this.id !== undefined) {
baseDto.id = this.id;
}
return baseDto;
}
}
Modify printers-posnet-card.vue to Bind targetId Instead of target:
Update the binding in printers-posnet-card.vue to use model.targetId instead of model.target.
vue
Ensure Proper Initialization:
Make sure that when loading data, model.targetId is correctly set from the DTO.
typescript
// In PrinterPosnet.fromDto
protected readDto(dto: any) {
super.readDto(dto);
// ... other assignments
this.targetId = dto.target ?? 0; // Ensure targetId is set
}
Adjust the submit Function:
Since model.targetId is now correctly bound and included in the DTO as target, the submit function should now work as expected, with dto.target containing the correct id.
Summary:
The core issue was a mismatch between the type expected by the PrinterPosnet model (PrinterPosnetTarget object) and what the printer-posnet-target-selector component was emitting (string name). By adjusting the selector to emit the id and modifying the model to handle target as an id, the dto.target will correctly hold the intended value, resolving the undefined issue during form submission.
Final Recommendation:
Ensure that the printer-posnet-target-selector component emits the target’s id instead of its name by setting item-value="id" in the <v-autocomplete>. Then, adjust your PrinterPosnet model to handle target as an id (number) rather than as a PrinterPosnetTarget object. This alignment will ensure that model.dto.target is correctly defined when submitting the form.