wailsapp / wails

Create beautiful applications using Go
https://wails.io
MIT License
25.34k stars 1.22k forks source link

[V2] runtime.EventsEmit not working with custom struct #1037

Closed steffenstolze closed 2 years ago

steffenstolze commented 2 years ago

Description

My "nested" struct Objekt is defined as this:

type Objekt struct {
    Nr           string
    Bezeichnung  string
    Strasse      string
    Plz          string
    Ort          string
    Mieter       []Mieter
    KontenSalden []KontoSaldo
}

type KontoSaldo struct {
    Konto int
    Saldo float64
}

I've got a slice of this struct var objekte []Objekt which I can pass perfectly fine via runtime.EventsEmit(app.ctx, "importEvent", objekte). I can also pass one specific item via runtime.EventsEmit(app.ctx, "importEvent", objekte[4]) with no problem. I receive the event and can console.log the data on the Vue side.

But as soon as I create a wrapper struct, since I want to add some more information to it, and try to pass it like this:

type ImportEvent struct {
    status  string   `json:"status"`
    objekte []Objekt `json:"objekte"`
}

var eventData ImportEvent
eventData.status = "Objekte importiert."
eventData.objekte = objekte
runtime.EventsEmit(app.ctx, "importEvent", eventData)

I always receive {} on the Vue side. When I change it to runtime.EventsEmit(app.ctx, "importEvent", eventData.objekte) it works again.

The code on the Vue side is very minimal:

    //handle Import Event
    const onImportEvent = (message) => {
      console.log(message)
    };

    //capture specific events from Go
    window.runtime.EventsOn('importEvent', onImportEvent);

Expected behaviour I'd expect the whole object of type ImportEvent on the Vue side.

System Details

Wails CLI v2.0.0-beta.23

Scanning system - Please wait (this may take a long time)...Done.

System
------
OS:             MacOS
Version:        12.1
ID:             199506
Go Version:     go1.17.5
Platform:       darwin
Architecture:   amd64

Dependency                      Package Name    Status          Version
----------                      ------------    ------          -------
xcode command line tools        N/A             Installed       2392
npm                             N/A             Installed       7.19.1
*upx                            N/A             Available

* - Optional Dependency

Diagnosis
---------
Your system is ready for Wails development!
Optional package(s) installation details: 
  - upx : Available at https://upx.github.io/

Thanks in advance! BR

leaanthony commented 2 years ago

Thanks for the detailed bug report! I think this might be because you aren't exporting your fields in the struct. Could you try that and let me know if that fixes it? Thanks!

type ImportEvent struct {
    Status  string   `json:"status"`
    Objekte []Objekt `json:"objekte"`
}
steffenstolze commented 2 years ago

Damn, this was so obvious and I missed it! Sorry for bothering you.

Exporting the fields made it work like a charm, perfect!

Thanks and BR!

gautamgiri-dev commented 1 year ago

@steffenstolze hello brother! I am trying to emit events from backend to frontend but am not able to find the EventsEmit function. How to call it?

Basically, my application requires a OAuth2 kind of authentication in which the app opens an authentication link in the users default browser from where after successfully logged in the browser redirects to a localhost http server which is being handled by the golang. Upon receiving the redirect I want to pass some data to frontend. How can I achieve it?

Do you know any solution?