I'm building a desktop app with Wails which writes to a local SQLite database.
I have the following model in my Go backend:
type FilmGroup struct { gorm.Model Films []FilmRoll FilmOrder FilmOrder Status *string Description *string Name *string}
I am using GORM as my ORM and the gorm.Model
parameter inserts some standard table fields into my model.
I have a function that adds an object of this model into my database:
func AddFilmGroup(db *gorm.DB, filmGroup models.FilmGroup) error { if err := db.Create(&filmGroup).Error; err != nil { return fmt.Errorf("could not add film group: %w", err) } log.Printf("FilmGroup '%d' added successfully.\n", filmGroup.ID) return nil}
And I have another function which is exposed to the frontend, that just calls the above function:
func (a *App) AddFilmGroupToDB(filmGroup models.FilmGroup) error { if a.database == nil { return fmt.Errorf("database connection is not initialized") } err := db.AddFilmGroup(a.database, filmGroup) if err != nil { return fmt.Errorf("failed to add film group: %w", err) } return nil}
In my Svelte frontend, I am trying to add a function to call this Go function so it can add to the database:
let newFilmGroup = { Name: "", Description: "", Status: "", }; const submitFilmGroup = async () => { try { const filmGroup = { Name: newFilmGroup.Name, Description: newFilmGroup.Description, Status: newFilmGroup.Status }; await AddFilmGroupToDB(filmGroup); closeAddFilmGroupModal(); } catch (error) { console.error("Error adding film group:", error); } };
newFilmGroup
will get populated with proper values by the UI through a form later, but for now just trying to test.
The issue is await AddFilmGroupToDB(filmGroup);
is expecting the ID and other fields as well. The error I get is:
Argument of type '{ Name: string; Description: string; Status: string; }' is not assignable to parameter of type 'FilmGroup'. Type '{ Name: string; Description: string; Status: string; }' is missing the following properties from type 'FilmGroup': ID, CreatedAt, UpdatedAt, DeletedAt, and 3 more.js(2345)const filmGroup: { Name: string; Description: string; Status: string;}
I'm not sure what I need to do to resolve this. This is just to create a new database entry for this model and it should only require the Name, Description, and Status fields to do so.
What change do I need to make to consume this function on my frontend properly?