The useGet
Method
The useGet
method takes the work out of watching an "id" variable and retrieving individual records from the API server.
Overview of Features
- Auto-Updating - change the
id
and it does the rest. - Fall-Through Cache - Always pulls from the store while new data is fetched.
- Easy Request State - Pending request state is built in.
- SSR Friendly - Data can load on the server and hydrate on the client.
Usage
You can call useGet
directly from the store. the advantage being that you don't have to provide the store
in the params, as shown here:
ts
interface Props {
id: string | number
}
const props = defineProps<Props>()
const { api } = useFeathers()
const id = computed(() => props.id)
const { data: user, isPending, error } = api.service('users').useGet(id)
For a client-only solution, you can just use the getFromStore
method:
ts
interface Props {
id: string | number
}
const props = defineProps<Props>()
const { api } = useFeathers()
const id = computed(() => props.id)
const user = api.service('users').getFromStore(id)
API
useGet(id, params)
id
{MaybeRef string | number} the id of the record to retrieve. Can be a computed/ref to enable automatic updates to the returneddata
.params
{Object} a combined FeathersParams
object and set of options for configuring behavior ofuseGet
.query
{Object} a Feathers query object.clones
{Boolean} returns result as a clone. See Querying Datatemps
{Boolean} enables retrieving temp records. See Querying Datawatch
{boolean} can be used to disable the watcher onid
immediate
{boolean} can be used to disable the initial request to the API server.
Returned Object
id
{Ref number | string} is a ref version of theid
that was provided as the first argument touseGet
. Modifyingid.value
will cause thedata
to change.params
{Params} is a ref version of the params. Params are not currently watched foruseGet
.data
{Computed Object} the record returned from the store.ids
{Ref Array} is a list of ids that have been retrieved from the API server, in chronological order. May contain duplicates.get
{Function} similar tostore.get
. If called without any arguments it will fetch/re-fetch the currentid
. Accepts no arguments.request
{Promise} stores the current promise for theget
request.requestCount
{Ref number} a counter of how many requests to the API server have been made.getFromStore
{Function} the same asstore.getFromStore
.isPending
{Computed boolean} returns true if there is a pending request. While true, thedata
will continue to hold the most recently-fetched record.hasBeenRequested
{Computed boolean} returns true if any record has been requested through this instance ofuseGet
. It never resets.hasLoaded
{Computed boolean} is similar toisPending
but with different wording.error
{Computed error} will display any error that occurs. The error is cleared if another request is made or ifclearError
is called.clearError
{Function} can be used to manually clear theerror
.
Only Query Once Per Record
The simplest way to only query once per record is to set the skipGetIfExists
option to true
during configuration.
You can also use the useGetOnce
method to achieve the same behavior for individual requests.