I'm encountering a TypeError when attempting to retrieve data from a backend Go to a frontend JavaScript application using the app binding method.
The method takes the name of a struct as an argument and casts it to the real Go struct using reflect.
While the backend functionality seems to work fine, I'm encountering an error in the frontend console when calling this method.
TypeError: Cannot destructure property 'order' of 'this.internalProps.sortState' as it is undefined.
To Reproduce
This code snippet is executed solely on the backend.
Please create a basic Wails application and integrate CallFromFrontend as a method of the app. Subsequently, invoke this method in the frontend, where you are likely to encounter the error in the console.
package main
import (
"fmt"
"reflect"
)
// Define the type registry to store model types by name
var typeRegistry = make(map[string]reflect.Type)
// Define a function to simulate data retrieval from the frontend
func CallFromFrontend(modelName string) (interface{}, error) {
// Get the type of the model from the typeRegistry
modelType, ok := typeRegistry[modelName]
if !ok {
return nil, fmt.Errorf("model type [%v] not found in the database 'typeRegistry'", modelName)
}
// Create a new instance of the model using reflection
modelPtr := reflect.New(modelType).Interface()
// Simulate fetching data from the database
// For the purpose of this reproduction, we'll create fake data here
switch modelName {
case "Item":
// Create a fake Item object and assign it to the model pointer
item := &Item{
Field1: "Fake Item Field1",
Field2: "Fake Item Field2",
Field3: "Fake Item Field3",
}
reflect.ValueOf(modelPtr).Elem().Set(reflect.ValueOf(item).Elem())
case "User":
// Create a fake User object and assign it to the model pointer
user := &User{
Field1: "Fake User Field1",
Field2: "Fake User Field2",
Field3: "Fake User Field3",
}
reflect.ValueOf(modelPtr).Elem().Set(reflect.ValueOf(user).Elem())
default:
return nil, fmt.Errorf("unsupported model name: %s", modelName)
}
//Dereference the `modelPtr`
modelValue := reflect.ValueOf(modelPtr).Elem()
if modelValue.Kind() == reflect.Ptr {
modelValue = modelValue.Elem()
}
rsVal := modelValue.Interface()
// Return the model pointer with fake data
return rsVal, nil
}
// Define the Item struct
type Item struct {
Field1 string `json:"field1"`
Field3 string `json:"field3"`
Field2 string `json:"field2"`
}
// Define the User struct
type User struct {
Field1 string `json:"field1"`
Field3 string `json:"field3"`
Field2 string `json:"field2"`
}
func main() {
// Define the models to be registered
ALL_MODELS := []interface{}{&Item{}, &User{}}
// Initialize the type registry with the model types
for _, v := range ALL_MODELS {
typeName := reflect.TypeOf(v).Elem().Name()
typeRegistry[typeName] = reflect.TypeOf(v).Elem()
}
// Call the function to simulate fetching data for the "Item" model
// For the re-production, PLEASE call this method in frontend
data, err := CallFromFrontend("Item")
if err != nil {
fmt.Println("Error:", err)
return
}
// Print the data returned from the function
// The backend WON'T have any error!!!!
fmt.Println("Data:", data)
}
Expected behaviour
I expect the data retrieved from the backend to be successfully processed in the frontend without encountering any errors.
Screenshots
No response
Attempted Fixes
No response
System Details
# Wails
Version | v2.8.1
# System
┌──────────────────────────────────────────────────────────────────────────────────────┐
| OS | Windows 10 Enterprise LTSC 2021 |
| Version | 2009 (Build: 19044) |
| ID | 21H2 |
| Go Version | go1.20.12 |
| Platform | windows |
| Architecture | amd64 |
| CPU | Intel(R) Core(TM) i7-6700 CPU @ 3.40GHz |
| GPU 1 | NVIDIA GeForce GTX 1050 (NVIDIA) - Driver: 31.0.15.1694 |
| GPU 2 | Intel(R) HD Graphics 530 (Intel Corporation) - Driver: 27.20.100.9664 |
| Memory | 32GB |
└──────────────────────────────────────────────────────────────────────────────────────┘
# Dependencies
┌───────────────────────────────────────────────────────┐
| Dependency | Package Name | Status | Version |
| WebView2 | N/A | Installed | 125.0.2535.92 |
| Nodejs | N/A | Installed | 20.10.0 |
| npm | N/A | Installed | 10.4.0 |
| *upx | N/A | Installed | upx 3.96 |
| *nsis | N/A | Available | |
└─────────────── * - Optional Dependency ───────────────┘
# Diagnosis
Optional package(s) installation details:
- nsis : More info at https://wails.io/docs/guides/windows-installer/
Description
I'm encountering a TypeError when attempting to retrieve data from a backend Go to a frontend JavaScript application using the
app
binding method. The method takes the name of a struct as an argument and casts it to the real Go struct usingreflect
. While the backend functionality seems to work fine, I'm encountering an error in the frontend console when calling this method.To Reproduce
This code snippet is executed solely on the backend. Please create a basic
Wails
application and integrateCallFromFrontend
as a method of theapp
. Subsequently, invoke this method in the frontend, where you are likely to encounter the error in the console.Expected behaviour
I expect the data retrieved from the backend to be successfully processed in the frontend without encountering any errors.
Screenshots
No response
Attempted Fixes
No response
System Details
Additional context
No response