#Command not working

75 messages · Page 1 of 1 (latest)

gloomy merlin
#

Unsure how I am going wrong here, but..
main.rs

#![cfg_attr(
    all(not(debug_assertions), target_os = "windows"),
    windows_subsystem = "windows"
)]

pub mod commands;
use crate::commands::greeting::greeting;

#[tauri::command]
fn greet(name: String) {
    greeting(&name);
}

fn main() {
    tauri::Builder::default()
        .invoke_handler(tauri::generate_handler![greet])
        .run(tauri::generate_context!())
        .expect("error whilst running tauri application");
}

commands/mod.rs

pub mod greeting;

commands/greeting.rs

pub fn greeting(name: &str) -> String {
    format!("Hello, {}! You've been greeted from Rust!", name)
}

app.tsx

async function greet() {
    setGreetMsg(await invoke("greet", { name }));
  }

Can you help?

shut barn
#

Any error message ?

gloomy merlin
#

Not on the rust side, on the client side of it in console I get

Uncaught (in promise) TypeError: window.__TAURI_IPC__ is not a function
    at chunk-5UWJICAP.js:1:529
    at new Promise (<anonymous>)
    at d2 (chunk-5UWJICAP.js:1:384)
    at greet (App.tsx:11:23)
    at Object.onSubmit [as submitfalse] (App.tsx:36:11)
    at HTMLFormElement.j (props.js:154:34)
shut barn
#

Maybe guest package and rust crate versions does not match

gloomy merlin
#

I just modified the basic vite template with the code above from a fresh download of rust and then tauri. Without my modifications above the function works. The only thing I've tried to do is modularise the commands into their own file.

gloomy merlin
#

If there is another example you can provide of achieving this I am open to it.

shut barn
#

I only saw this error when guest js and the rust part version missmatch

#

so if that not that I have no clue

gloomy merlin
#

Thanks for looking. Is the approach to making them modular/seperate files OK though? Would you do it any differently?

wheat magnet
#

Ipc not found like that means either you opened your frontend in a regular browser or you have some version nr issues. The first one is the more likely

#

Third options is a really old mac version

gloomy merlin
#

Well, I did check it in the browser as the command didn't work in the app but gave no error, was just blank

gloomy merlin
#

Thats where I saw the error in the browser at least, in the console

wheat magnet
#

Same keybinding to open as in the browser

#

You should check devtools inside the tauri app instead

#

Tauri apps dont work in a browser

#

So run you app and when you see the north pole on your screen press the same keybinding as in your browser

#

On windows it's ctrl + shift + i

gloomy merlin
#

console is blank when I try the standard greet function...

wheat magnet
#

Then there pretty much has to be an error in the terminal. There's either an error in the windows devtools or in the terminal

gloomy merlin
wheat magnet
#

What's the output of tauri info?

#

npm run tauri info

gloomy merlin
wheat magnet
#

Modify the js to instead console.log the info. Im not great with preact but lookikg at the template and assuming that's the same as what you have then I think the trmplate may just be broken on the showing part

#

On my phone atm so cant verify

gloomy merlin
#

Comes back undefined

#

When submitting the form

#
console.log(setGreetMsg(await invoke("hello", { name })));
wheat magnet
#

Why does that say invoke("hello")

#

It should be greet

gloomy merlin
#

Oh, sorry, I changed things around a bit...

#
pub mod commands;
use crate::commands::greet::greet;

#[tauri::command]
fn hello(name: String) {
    greet(&name);
}
#
fn main() {
    tauri::Builder::default()
        .invoke_handler(tauri::generate_handler![hello])
        .run(tauri::generate_context!())
        .expect("error whilst running tauri application");
}
#

commands/mod.rs

pub mod greet;
#

commands/greet.rs

pub fn greet(name: &str) -> String {
    format!("Hello, {}! You've been greeted from Rust!", name)
}
#

I probably messed something up when toying around with it tbh

#

In the original template with the function defined in main.rs, it works fine.

wheat magnet
#

Yea you removed the return from the function

#

So yea no wonder it didnt return anything

gloomy merlin
#

The original function was:

#[tauri::command]
fn greet(name: &str) -> String {
    format!("Hello, {}! You've been greeted from Rust!", name)
}
wheat magnet
#

Yep, which has a return

#

Your hello function does not

#

The -> String in Rust isnt just a type hint. There wont be a return without it

#

In TS you can screw that up and still get something because it's just a hint

#

In Rust not so much

gloomy merlin
#

Can you advise how to modify the hello function because I did try

fn hello(name: &str) -> String {
    greet(&name);
}
``` and it just errors
#

oh ffs

#

I was so close

wheat magnet
#

Yea because you are using ; to suppress the return of the greet function

gloomy merlin
#

I removed semi colon

#

It works now lmao

wheat magnet
#

Yep

gloomy merlin
#

Rust is weird man

wheat magnet
#

In TS/JS ; is a stylistic choice
In Rust it's an output suppressor

gloomy merlin
#

Ahh

#

I can't believe I was just a semi colon away before....

#

Spent hours on this

wheat magnet
#

We're all new at some point 🤗

gloomy merlin
#

At least I know in future if I get undefined its not set to return a value

#

Thank you so much for the help with this

#

Infact wait

#

Lemme just make sure I'm not being an idiot, because I didn't remove the console log but

#

bit*

#

What's confusing me there is it was still set to console.log it, but it works?

#

And dev tools still says undefined

#

Am I being an idiot?

modern pecan
#
function x() {
   const [greetMsg, setGreetMsg] = useState("");

        ...
        const x = setGreetMsg("test");
        // x == undefined
        ...

   ...
}
#

if you want to print the value then just do something like

const newName = await invoke("hello", { name });
console.log(newName);
setGreetMsg(newName);
gloomy merlin
#

It was more that I had the entire invoke wrapped in a console log and it still worked when I pressed the form. But thanks guys, really appreciate it