How does the ServerAction obtain the result of the action in the SSR mode? #3648
-
I use the Resource to listen to action.version and then call other server functions. Then, I read the data returned by the server functions in the Transition component. However, when I use the Resource to listen to action.version and try to obtain action.value, I can't get the data. let user = Resource::new(
move || login_action.version()(),
move |_| user_detail(),
);
let login_res = Resource::new(
move || login_action.version()(),
move |_| async move {
login_action.value()()
},
); I also tried several other methods, but still couldn't get the data. let i_e = RwSignal::new("".to_string());
let n_e = RwSignal::new("".to_string());
let w_e = RwSignal::new("".to_string());
let login_value = Signal::derive(move || {
match login_action.value()() {
Some(res) => match res {
Ok(_) => Some("login success".to_string()),
Err(e) => Some(e.to_string()),
},
None => None,
}
});
let login_memo = Memo::new(move |_| {
match login_action.value()() {
Some(res) => match res {
Ok(_) => Some("login success".to_string()),
Err(e) => Some(e.to_string()),
},
None => None,
}
});
Effect::new_isomorphic(move || {
if let Some(r) = login_action.value()() {
if let Err(e) = r {
leptos::logging::log!("login res:{e}");
i_e(e.to_string());
}
}
});
Effect::new(move || {
if let Some(r) = login_action.value()() {
if let Err(e) = r {
leptos::logging::log!("login res:{e}");
n_e(e.to_string());
}
}
});
Effect::watch(
move || login_action.value()(),
move |curr, prev, _| {
if let Some(r) = curr {
if let Err(e) = r {
leptos::logging::log!("login res:{e}");
w_e(e.to_string());
}
}
},
true,
);
view! {
<main>
<Transition fallback=|| "Loading...">
{move || {
user.get().map(|u| match u {
Err(e) => view! {
<p>error:{e.to_string()}</p>
//get user info error, redirect to login page
<Show when=move || use_url()().path() != "/login">
<Redirect path="/login" />
</Show>
}.into_any(),
Ok(u) => {
global_state.logged().set(true);
view! { <p>id:{u.user_id}</p> }.into_any()
}
})
}}
<p>ie:{i_e}</p>
<p>ne:{n_e}</p>
<p>we:{w_e}</p>
<p>d:{login_value}</p>
<p>m:{login_memo}</p>
<p>r:{move || {
match login_res.get().flatten() {
Some(r) => match r {
Ok(_) => view! { "login success" }.into_any(),
Err(e) => view! { <p>{e.to_string()}</p> }.into_any(),
},
None => view! { "None" }.into_any(),
}
}}</p>
</Transition>
</main>
} |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
By looking at #2069 I learned that actively calling the redirect function on server fn can prevent the default |
Beta Was this translation helpful? Give feedback.
-
I'm sorry, I'm not sure I understand what you mean by "get the data." Sticking with your first example: What are you expecting to happen, and what happens instead? I would assume |
Beta Was this translation helpful? Give feedback.
-
I want to catch the exception when there is an exception in |
Beta Was this translation helpful? Give feedback.
By looking at #2069 I learned that actively calling the redirect function on server fn can prevent the default
redirect
of the form and get the response from server fn for subsequent processing