Hi guys, I have a question about Client / Server functions.
My app is currently running at the client level. The client api calls are all pointed to the public url. I want to change it so the client calls function on the server. I want the client to send a text string from an input box to a server function. then the client waits for a response. I can provide the current working code if you think it would help.
7 Replies
Hi @johne5. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
To answer your question about client/server functions in Wasp, let's focus on how you can make the client call a function on the server and send data from an input box. Wasp provides a straightforward way to achieve this using Actions.
Here's how you can implement this in Wasp:
1. First, you need to declare an Action in your Wasp file:
2. Then, implement the server-side logic in your
src/actions.js
file:
3. On the client side, you can use this Action like this:
This setup allows you to send text from the client to a server function and wait for a response. The client calls the processText
Action, which runs on the server, and then receives the result.
Note that you don't need to worry about setting up API endpoints or managing URLs. Wasp handles all of that for you behind the scenes.
For more detailed information on working with Actions in Wasp, you can refer to the Working with Actions documentation.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
Sorry @johne5, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist:
Perhaps try again in 15mins?Did the bot's answer help you? 🙂
on step 1, what file do i need to add that code too? main.wasp?
@kapa.ai router.tsx:31 Uncaught SyntaxError: The requested module '/@fs/home/johne5/website/app/src/client/testserverside.jsx' does not provide an export named 'TestServerSidePage' (at router.tsx:31:10)
Let me check the knowledge base for you again. One moment! :books:
Wohooo @johne5, you just became a Waspeteer level 4!
----------------------
This error message indicates that there's an issue with importing a named export from a module in your Wasp project. Let's break down the problem and provide a solution based on the information in the knowledge sources.
The error suggests that the file
testserverside.jsx
doesn't export a component named TestServerSidePage
. This is likely due to a mismatch between how you're exporting the component and how you're trying to import it.
To resolve this issue, you need to ensure that your export in testserverside.jsx
matches the import in router.tsx
. Here are the steps to fix it:
1. Check your testserverside.jsx
file:
Make sure you're exporting the component correctly. It should be a named export, like this:
2. In your router.tsx
file:
Ensure you're importing the component correctly. It should look like this:
Note that the path might be different depending on your project structure.
3. If you're using a default export instead of a named export, you would need to change your import statement to:
And in testserverside.jsx
:
Remember that Wasp uses named exports by default for components, as mentioned in the Wasp documentation on Pages & Routes:
If you're still having issues, double-check that the file path is correct and that there are no typos in the component name.
For more information on how Wasp handles imports and exports, you can refer to the Wasp documentation on Pages & Routes.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
Sorry @johne5, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist:
Perhaps try again in 15mins?ok, i think i got the sample code working
the route needed to import "MyComonent".
route TestServerSideRoute { path: "/testserverside", to: TestServerSidePage } page TestServerSidePage { component: import { MyComponent } from "@src/client/testserverside" }
route TestServerSideRoute { path: "/testserverside", to: TestServerSidePage } page TestServerSidePage { component: import { MyComponent } from "@src/client/testserverside" }