Type return error when using mongoose.
node - v16.15.1
npm
I'm somewhat new to trpc. Using it with mongoose. Love it so far althought I do have a problem with how types are returned.
For example I have a simple procedure:
This should return either UserDocument or null.
I call it in my client:
But this gives error:
So it seems to be returning a different type than specified.
npm
I'm somewhat new to trpc. Using it with mongoose. Love it so far althought I do have a problem with how types are returned.
For example I have a simple procedure:
export const getUser = procedure
.input(
z.object({
walletAddress: z.string(),
})
)
.mutation(async (opts) => {
const db = await connectDB();
const user: UserDocument | null = await User.findOne({
walletAddress: opts.input.walletAddress,
});
return user;
});export const getUser = procedure
.input(
z.object({
walletAddress: z.string(),
})
)
.mutation(async (opts) => {
const db = await connectDB();
const user: UserDocument | null = await User.findOne({
walletAddress: opts.input.walletAddress,
});
return user;
});This should return either UserDocument or null.
I call it in my client:
const getUser = trpc.getUser.useMutation();
const currentUser: UserDocument | null = await getUser.mutateAsync({
walletAddress: userWalletAddress
})const getUser = trpc.getUser.useMutation();
const currentUser: UserDocument | null = await getUser.mutateAsync({
walletAddress: userWalletAddress
})But this gives error:
Type '{ walletAddress: string; _id: string; createdAt?: string | undefined; updatedAt?: string | undefined; readonly URL: string; alinkColor: string; readonly all: { [x: number]: { id: string; onfullscreenchange: null; ... 97 more ...; readonly assignedSlot: { ...; } | null; }; readonly length: number; }; ... 196 more ......' is not assignable to type 'UserDocument | null'.
Type '{ walletAddress: string; _id: string; createdAt?: string | undefined; updatedAt?: string | undefined; readonly URL: string; alinkColor: string; readonly all: { [x: number]: { id: string; onfullscreenchange: null; ... 97 more ...; readonly assignedSlot: { ...; } | null; }; readonly length: number; }; ... 196 more ......' is missing the following properties from type 'UserDocument': adoptNode, captureEvents, caretRangeFromPoint, clear, and 66 more.Type '{ walletAddress: string; _id: string; createdAt?: string | undefined; updatedAt?: string | undefined; readonly URL: string; alinkColor: string; readonly all: { [x: number]: { id: string; onfullscreenchange: null; ... 97 more ...; readonly assignedSlot: { ...; } | null; }; readonly length: number; }; ... 196 more ......' is not assignable to type 'UserDocument | null'.
Type '{ walletAddress: string; _id: string; createdAt?: string | undefined; updatedAt?: string | undefined; readonly URL: string; alinkColor: string; readonly all: { [x: number]: { id: string; onfullscreenchange: null; ... 97 more ...; readonly assignedSlot: { ...; } | null; }; readonly length: number; }; ... 196 more ......' is missing the following properties from type 'UserDocument': adoptNode, captureEvents, caretRangeFromPoint, clear, and 66 more.So it seems to be returning a different type than specified.