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.
2 Replies
I haven't used mongoose for about ten years but IIRC it has quite fat instance objects with methods on them. It's possible that they are not serializable through JSON and that it's why it fails
Oh, this is a typescript error
Just remove the type assertions
E.g.
Dont set the return value, we will transform it to an equivalent of what JSON stringify does
When I hover over user it does give me the json of all values it returns, but that is not a defined type. So I if there are other function that need a specific type like UserDocument, how do I make sure that this is UserDocument?
So for example I have another trpc procedure:
const newDeck = await createNewDeck.mutateAsync({
deckName: deckName,
walletAddress: user?.walletAddress ?? '',
image: imageUrl
})
When I hover over newDeck I can see the values:
But there is no definition of the type so when I want to use it in another method like this one:
playerDeckActions.setAddDeckToList(newDeck)
The method expects DeckDocument but newDeck is not recognized as such:
function setAddDeckToList(deck: DeckDocument) {
const currentDeckList = JSON.parse(JSON.stringify(playerDecksState.playerDecks));
currentDeckList.push(deck)
setPlayerDecks((state: any) => ({
...state,
playerDecks: [...currentDeckList],
}))
}
and so it will throw a same error: