I have a Post Model:
const PostSchema = new Schema<IPost>(
{
// ...
likes: [{ type: Schema.Types.ObjectId, ref: "User" }],
// ...
}
)
export default model<IPost>("Post", PostSchema)
export interface IPost {
// ...
likes: ObjectId[]
// ...
}
export interface IPostDocument extends Document, IPost {}
And I'm trying to toggle a user like:
export const toggleLike: TController = async (req, res, next) => {
const user = req.user as IUserDocument;
const userId = user._id;
const postId = req.params.postId;
try {
const disliked = await PostModel.findOneAndUpdate(
{ _id: postId, likes: userId },
{ $pull: { likes: userId } }
); // works with no problem
if (disliked)
res.json({ message: `User ${userId} disliked post ${postId}` });
else {
const liked = await PostModel.findOneAndUpdate(
{ _id: postId },
{ $push: { likes: userId } }
); // the $push throws an error "Type instantiation is excessively deep and possibly infinite."
if (liked) res.json({ message: `User ${userId} liked post ${postId}` });
else return next(createError(404, "Post not found"));
}
} catch (error) {
next(createError(500, error as Error));
}
};
The mongo $push operator is throwing an error "Type instantiation is excessively deep and possibly infinite."
I doubt it helps but the description of the error is:
(property) likes?: _AllowStringsForIds<(((((((((((... | any[])[] | any[])[] | any[])[] | any[])[] | any[])[] | any[])[] | any[])[] | any[])[] | any[])[] | any[])[] | any[])[] | any[]> | ArrayOperator<(_AllowStringsForIds<(((((((((((... | any[])[] | any[])[] | any[])[] | any[])[] | any[])[] | any[])[] | any[])[] | any[])[] | any[])[] | any[])[] | any[])[] | any[]> | undefined)[]> | undefined
Any idea what's happening?