juicoJ
tRPC3y ago
7 replies
juico

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:

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
})

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.


So it seems to be returning a different type than specified.
Was this page helpful?