If an error is thrown by data(), then Vike renders your error page and there is usually nothing for you to do (beyond defining an error page /pages/_error/+Page.js).
But if you want a more precise error handling (such as showing an insightful error message to the user instead of some generic "Something went wrong"), then use throw render() and/or throw redirect().
By default, the data() hook always runs the server-side. Thus you can directly use ORM/SQL database queries:
Client-side
By default, the data() hook always runs on the server-side.
But,
by using the file extension .shared.js (renaming +data.js to +data.shared.js),
you can tell Vike to run data() also on the client-side. This will make Vike:
Call data() on the server-side for the first page the user visits.
Call data() on the client-side for subsequent page navigations.
In general, we recommend running data() only on the server-side because it's usually easier to write code that runs only on the server-side.
That said, if you want to minimize requests made to your server, then it can make sense to run data() on the client-side. See Avoid pageContext.json requests.
You can also set .client.js instead of .shared.js: Vike will then always call data() on the client-side (never on the server-side).
Alternatively, instead of using .shared.js and .client.js, you can modify the meta.env setting of the data() hook in a global fashion, removing the need to add .shared.js / .client.js to each of your +data.js files.
pageContext.data
The data() hook sets the value of pageContext.data. (While useData() exposes the value of pageContext.data.)
This means that, beyond using useData(), you can also access the data in other hooks over pageContext.data.
Without useData()
The data() hook is usually used together with the component hook useData() which is provided by the UI framework Vike extension (vike-react/vike-vue/vike-solid).
In general, for improved DX, we recommend using data() together with a useData() implementation.