简体   繁体   中英

Mongoose/Typescript issue

So, i have to update some data inside my main interface, the problem is that, when i tried to do that, it complains because .save() is not defined

So i create another interface to that data in order to extends Document so i can have access to.save()

But, here's the new error....

const theComment: IComment
Type 'Comment' is missing the following properties from type 'IComment': $getAllSubdocs, $ignore, $isDefault, $isDeleted, and 47 more.

Here's my code

What i want to update ( the problem is theComment )

export const editComment = async (req: Request, res: Response) => {

  const {publicationId} = req.params;

  const { identifier, body, commentId } = req.body;

  // Check id's

  if (!mongoose.Types.ObjectId.isValid(identifier!))
    return res.status(400).json({ Message: "identifier not valid" });

  if (!mongoose.Types.ObjectId.isValid(publicationId!))
    return res.status(400).json({ Message: "identifier not valid" });

    if (!mongoose.Types.ObjectId.isValid(commentId!))
    return res.status(400).json({ Message: "identifier not valid" });
  
  // Find pub

  const thePub: Ipub = await Publication.findById(publicationId);

  // Find user

  const theUser: Iauth = await User.findById(identifier);

  // Find comment, make sure that comment is from that user

  const theComment: IComment = thePub.comments!.find((f) => f.id === commentId && f.identifier === theUser.id)!;

  if(!theComment) return res
  .status(405)
  .json({ Message: "You are not the owner of the comment || Comment doesn't exist" })

  // Make sure body is not empty

  if(!body) return res.status(404).json({Message: 'No data provided'})

  try {

    // Update comment and perfil if it has changed

    theComment.body = body;
    theComment.perfil = theUser.perfil;

    await theComment.save()

    return res.json(theComment)

  } catch (err) {
    console.log(err);
    return res.status(500).json({ Error: "the API failed" });
  }
};

Main interface

export interface Ipub extends Document {
  id?: string;
  body: string;

  photo: string;

  creator: {
    name: string;
    perfil?: string;
    identifier: string;
  };

  likes?: Likes[];

  comments?: Comment[];

  createdAt: string;
}

Data's interface that i want to update inside my main interface

export interface IComment extends Document {
  id?: string;
  body: string;
  name: string;
  perfil?: string;
  identifier: string;
  createdAt: string;
  likesComments?: Likes[];
}

What can i do? how can i solve it?

Thanks for your time comunnity !!

TS Compiler says the object described by Comment interface doesn't have .save() method. And as far as I presume it should not have because it's not a MongoDB document.

The time you inherit all props from Document interface the compiler throws the error saying that types Comment & IComment are not compatible because the second one has Document props, and the first one doesn't. To fix it you should just cast the type directly like this: const theComment = thePub.comments..find((f) => f.id === commentId && f.identifier === theUser;id)! as IComment; const theComment = thePub.comments..find((f) => f.id === commentId && f.identifier === theUser;id)! as IComment;

But in order to update the comment you have to update 'whole' Publication document(for example, by using aggregate ):

Publication.update(
  {
    "id": publicationId, 
    "comments.id": commentId,
    "comments.identifier": theUser.id,
  }, 
  { $inc: {
    "comments.$.body": body,
    "comments.$.perfil": theUser.perfil,
  }}, 
  false, 
  true,
);

Or the best option I think is to use relationships between Documents. Create another Document named Comment and save all related comments there. In that case you will have an ability to use .save() and other methods provided.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM