How to get current user for inline template values? #3777
-
Hi {
name: "redaktor",
title: "Redaktører",
type: "array",
description:
"Her velger du hvem som eier innholdet i denne tiltaksgjennomføringen.",
to: [{ type: "redaktor" }],
of: [
{
type: "reference",
to: [{ type: "redaktor" }],
},
],
validation: (Rule) => Rule.required().unique(),
initialValue: async () => {
const user = await userStore.getCurrentUser(); // TODO How to get current user in v3 without a React-component
const foundRedaktor = await client.fetch( // TODO How to fetch using a client in the context of the studio without a React-component
`*[_type == "redaktor" && navn == '${user.name}'][0]`
);
if (!foundRedaktor) return [];
return [
{
_type: "reference",
_ref: foundRedaktor?._id,
},
];
},
}, And in v2 I use the initialValue property to get the current user, look up some data via the client and then connect a reference if the data is found. I am struggling with how to do this with v3. All the documentations points to using the |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
The {
type: "reference",
to: [{ type: "redaktor" }],
},
],
validation: (Rule) => Rule.required().unique(),
initialValue: async (params, context) => {
const {currentUser: user, getClient} = context
const client = getClient({apiVersion: '2022-10-20'})
const foundRedaktor = await client.fetch( // TODO How to fetch using a client in the context of the studio without a React-component
`*[_type == "redaktor" && navn == '${user.name}'][0]`
);
if (!foundRedaktor) return [];
return [
{
_type: "reference",
_ref: foundRedaktor?._id,
},
];
},
}, |
Beta Was this translation helpful? Give feedback.
The
initialValue
callback function should now receive a context object that includes the current user. I haven't tested this code, but it should be something like this: