【问题标题】:Mongoose/Typescript issue猫鼬/打字稿问题
【发布时间】:2021-05-31 05:13:59
【问题描述】:

所以,我必须在我的主界面中更新一些数据,问题是,当我尝试这样做时,它会抱怨因为 .save() 没有定义

所以我为该数据创建了另一个接口,以便extends Document 这样我就可以访问.save()

但是,这是新的错误......

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

这是我的代码

我要更新的内容(问题是评论)

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

主界面

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

  photo: string;

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

  likes?: Likes[];

  comments?: Comment[];

  createdAt: string;
}

我想在主界面中更新的数据界面

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

我能做什么?我该如何解决?

感谢您的时间社区!!

【问题讨论】:

    标签: node.js typescript mongoose


    【解决方案1】:

    TS 编译器说Comment 接口描述的对象没有.save() 方法。而且据我推测它不应该有,因为它不是 MongoDB 文档。

    当您从Document 接口继承所有道具时,编译器会抛出错误,指出类型CommentIComment 不兼容,因为第二个具有Document 道具,而第一个没有。要修复它,您应该像这样直接转换类型: const theComment = thePub.comments!.find((f) => f.id === commentId && f.identifier === theUser.id)! as IComment;

    但要更新评论,您必须更新“整个”Publication 文档(例如,使用 aggregate):

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

    或者我认为最好的选择是使用文档之间的关系。创建另一个名为Comment 的文档并将所有相关的cmets 保存在那里。在这种情况下,您将能够使用.save() 和提供的其他方法。

    【讨论】:

    • 谢谢,我所做的,我用 .save() 保存了整个出版物并且它工作了,但是感谢你的声明,需要继续提高我的打字技能!
    猜你喜欢
    • 2017-04-11
    • 2021-10-05
    • 2016-02-05
    • 2020-05-11
    • 2016-04-01
    • 2017-01-20
    • 2021-01-05
    • 1970-01-01
    • 2016-06-14
    相关资源
    最近更新 更多