wailsapp / wails

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

TypeError: window.go is undefined #2832

Closed devalexandre closed 1 year ago

devalexandre commented 1 year ago

Description

when I use sqlite display the message TypeError: window.go is undefined.

To Reproduce

  1. create a sqlite database with table sql
    CREATE TABLE Tasks (
    id INTEGER PRIMARY KEY,
    titulo CHAR(100) not null,
    estimativa INTEGER not null default 1,
    criacao DATETIME not null default NOW
    );

sqlite connect internal/database.go

package internal

import (
    "context"
    "fmt"

    "github.com/vingarcia/ksql"
    "github.com/vingarcia/ksql/adapters/ksqlite3"
)

type SQLite struct {
    ksql.DB
}

func NewSQLQuery(path string) *SQLite {
    db, err := ksqlite3.New(context.Background(), path, ksql.Config{
        MaxOpenConns: 1,
    })

    if err != nil {
        fmt.Println("Erro ao conectar com o banco de dados", err)
        panic(err)
    }

    return &SQLite{
        db,
    }
}

create folter internal/reposioty and create file task.go

package repository

import (
    "context"
    "devtasks/internal"
    "fmt"
    "time"

    "github.com/vingarcia/ksql"
)

var TasksTable = ksql.NewTable("Tasks")

type Tasks struct {
    ID         int       `json:"id" ksql:"id"`
    Titulo     string    `json:"titulo" ksql:"titulo"`
    Estimativa int       `json:"estimativa" ksql:"estimativa"`
    Criacao    time.Time `json:"criacao" ksql:"criacao,timeNowUTC"`
}

type PartialUpdateTask struct {
    ID         int     `json:"id" ksql:"id"`
    Titulo     *string `json:"titulo" ksql:"titulo"`
    Estimativa *int    `json:"estimativa" ksql:"estimativa"`
}

type TasksRepository struct {
    internal.SQLite
}

func NewTasksRepository(db *internal.SQLite) *TasksRepository {
    return &TasksRepository{
        *db,
    }
}

func (t *TasksRepository) GetTasks() ([]Tasks, error) {
    var tasks []Tasks
    err := t.Query(context.Background(), &tasks, "FROM Tasks")
    if err != nil {
        return tasks, err
    }

    fmt.Println(tasks)
    return tasks, nil
}

main.go

package main

import (
    "devtasks/internal"
    "devtasks/internal/repository"
    "embed"

    "github.com/wailsapp/wails/v2"
    "github.com/wailsapp/wails/v2/pkg/options"
    "github.com/wailsapp/wails/v2/pkg/options/assetserver"
)

//go:embed all:frontend/dist
var assets embed.FS

func main() {
    // Create an instance of the app structure
    db := internal.NewSQLQuery("db.sqlite")
    //defer db.Close()

    app := NewApp()
    task := repository.NewTasksRepository(db)

    // Create application with options
    err := wails.Run(&options.App{
        Title:  "Dev Tasks",
        Width:  1024,
        Height: 768,
        AssetServer: &assetserver.Options{
            Assets: assets,
        },
        BackgroundColour: &options.RGBA{R: 27, G: 38, B: 54, A: 1},
        OnStartup:        app.startup,
        Bind: []interface{}{
            app,
            task,
        },
    })

    if err != nil {
        println("Error:", err.Error())
    }
}

app.jsx

import './app.css';
import { GetTasks } from "../wailsjs/go/repository/TasksRepository";
import { useEffect, useState } from "preact/hooks";

export function App(props) {

  const [filteredData, setFilteredData] = useState([]);

  function getTasks() {
    GetTasks()
      .then((tasks) => {
        console.log("tasks",tasks);
        setFilteredData(tasks);
      }
    );
  }

  useEffect(() => {
    getTasks();
    console.log("filteredData",filteredData);

  }, [filteredData])

  return (
    <>
      <div id="App">
       <h1>Alexandre</h1>
      </div>
    </>
  )
}

Expected behaviour

return a array with Tasks

Screenshots

image

Attempted Fixes

not fixed :(

System Details

Wails CLI v2.5.1

 SUCCESS  Done.                                                                                                                

# System

OS           | ArcoLinux
Version      | Unknown  
ID           | arcolinux
Go Version   | go1.18   
Platform     | linux    
Architecture | amd64    

# Wails

Version         | v2.5.1
Package Manager | pacman

# Dependencies

Dependency | Package Name | Status    | Version    
*docker    | docker       | Installed | 1:24.0.5-1 
gcc        | gcc          | Installed | 13.2.1-3   
libgtk-3   | gtk3         | Installed | 1:3.24.38-1
libwebkit  | webkit2gtk   | Installed | 2.40.5-1   
npm        | npm          | Installed | 9.8.0      
pkg-config | pkgconf      | Installed | 1.8.1-1

Additional context

No response

ivila commented 1 year ago

@devalexandre image It might because you are using a wrong port, in your snapshots, I see you are using port 5173, but actually you should try 34115 instead.