【问题标题】:Mongoose don't insert object into array猫鼬不将对象插入数组
【发布时间】:2022-01-07 14:16:16
【问题描述】:

我有一个简单的快速应用程序,可以将 cmets 插入帖子,问题是 cmets 从未插入,但通过邮递员发布时没有显示错误,它正确返回帖子但没有 cmets。 试试看:thisthis 但似乎不起作用

这是我的架构

interface PostAttrs {
  userid: mongoose.Schema.Types.ObjectId;
  username: string;
  date: Date;
  text: string;
  image: string;
  comments: Array<any>;
  likes?: number;
}

const postSchema = new Schema<PostAttrs>({
  userid: {
    type: mongoose.Schema.Types.ObjectId,
    required: true,
  },
  username: {
    type: String,
    required: true,
  },
  date: {
    type: Date,
    required: true,
  },
  text: {
    type: String,
    required: true,
  },
  image: {
    type: String,
    required: false,
  },

  comments: [
    {
      required: false,
      date: {
        type: String,
        required: true,
      },
      user: {
        type: Schema.Types.ObjectId,
        ref: 'User',
        required: true,
      },
      text: {
        type: String,
        required: true,
      },
    },
  ],

  likes: {
    type: Number,
    required: true,
  },
});

还有 API 路由

export const createComment = async (req: Request, res: Response) => {
  try {
    const postId = req.params.postId;
    const userId = req.params.userId;
    const comment = req.body.comment;
    var commentObj = {
      date: new Date(),
      userId: userId,
      text: comment
    };
    await Post.findOneAndUpdate(
      { _id: postId }, 
      { new: true },
      {$push: {
        comments: { commentObj }
      }},
      (err: any, doc: any) => {
        if (err) {
          console.log("Something wrong when updating data!");
        }
        console.log(doc);
        return res.status(200).send(doc);
      }
      );

  } catch (error) { }
}

我的代码有什么问题?

【问题讨论】:

    标签: node.js typescript mongodb express mongoose


    【解决方案1】:

    已解决:问题是 findOneAndUpdate() 语句中参数的顺序,首先是搜索条件,其次是要更新的值,最后是语句。所以我不得不改变这个

    await Post.findOneAndUpdate(
          { _id: postId }, 
          { new: true },
          {$push: {
            comments: { commentObj }
          }},
          (err: any, doc: any) => {
            if (err) {
              console.log("Something wrong when updating data!");
            }
            console.log(doc);
            return res.status(200).send(doc);
          });
    

    await Post.findOneAndUpdate(
          { _id: postId }, 
          {$push: {
            comments: { commentObj }
          }},
          { new: true },
          (err: any, doc: any) => {
            if (err) {
              console.log("Something wrong when updating data!");
            }
            console.log(doc);
            return res.status(200).send(doc);
          });
    

    【讨论】:

      【解决方案2】:

      当使用 'await' 和 Mongoose 的方法(如 findOneAnd....)时,除非您明确这样做,否则该方法不会运行。

      试试:

      await Post.findOneAndUpdate(......).exec();
      

      此外,当使用 await 关键字时,您可以重构并删除回调

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-12
        • 1970-01-01
        • 2018-02-21
        • 2015-12-28
        • 2021-05-06
        • 1970-01-01
        相关资源
        最近更新 更多