最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

如何用在猫鼬中具有用户ID的帖子填充用户?

IT培训 admin 14浏览 0评论

如何用在猫鼬中具有用户ID的帖子填充用户?

我正在创建我的第一个MERN应用程序,并且遇到了这个问题:我的帖子包含用户ID:

{
    "reward": {
        ...
    },
    "_id": "5eb2d90d7d56c415cc4d5f97",
    "user": "5eabbb85b8814723fcee0f01",
    "text": "Body text hihi hahha",
    "createdAt": "2020-05-06T15:34:37.439Z",
    "__v": 0
}

我在帖子控制器中创建时确实设法自动将用户ID添加到帖子中。现在的问题是,我不知道应该在何处以及如何在用户模型的帖子数组中推送创建的帖子的ID。这是用户模型:

    username: {
    type: String,
    required: [true, "Username is required"],
    unique: true,
    trim: true,
    minlength: 3,
    maxlength: 15,
  },
  email: {
    type: String,
    required: [true, "Email is required"],
    unique: true,
  },
  role: {
    type: String,
    enum: ["user"],
    default: "user",
  },
  cards: [
    {
      type: mongoose.Schema.ObjectId,
      ref: "Card"
    },
  ],

[就在“卡片”所在的位置,我想在用户字段中包含保存用户ID的帖子的ID。这是Post模型:

    user: String,
  text: {
    type: String,
  },
  reward: {
    ...
  },
  user: {
    type: mongoose.Schema.ObjectId,
    ref: 'User',
    required: true
  },

这是将帖子保存到数据库的控制器:

exports.addCard = asyncHandler(async (req, res, next) => {
    //Add user to req.body
    req.body.user = req.user.id;

    const card = await Card.create(req.body);

    res.status(201).json({
        success: true, 
        message: 'Card created', 
        data: card
    });
});

[我想这应该在创建帖子时发生,就像在当前登录用户的用户模式下将帖子ID推送到帖子数组中一样?如何正确存档?

回答如下:

您可以进行第二次查询以插入_id

确保_id处的$match是类似于ObjectId("5d345234daa45")而不是5d345234daa45的ObjectId

exports.addCard = asyncHandler(async (req, res, next) => {
    //Add user to req.body
    req.body.user = req.user.id;

    const card = await Card.create(req.body);

    const id = card._id; // this should be your _id,  check with console.log() if this is your _id

    //dont forget to import your model 
    await User.aggregate([
       {
          $match: {
             _id: mongoose.Types.ObjectId(req.body.user)
          }
       },
       {
          $push: {
             cards: id
          }
       }
    ]);


    res.status(201).json({
        success: true, 
        message: 'Card created', 
        data: card
    });
});

如何用在猫鼬中具有用户ID的帖子填充用户?

我正在创建我的第一个MERN应用程序,并且遇到了这个问题:我的帖子包含用户ID:

{
    "reward": {
        ...
    },
    "_id": "5eb2d90d7d56c415cc4d5f97",
    "user": "5eabbb85b8814723fcee0f01",
    "text": "Body text hihi hahha",
    "createdAt": "2020-05-06T15:34:37.439Z",
    "__v": 0
}

我在帖子控制器中创建时确实设法自动将用户ID添加到帖子中。现在的问题是,我不知道应该在何处以及如何在用户模型的帖子数组中推送创建的帖子的ID。这是用户模型:

    username: {
    type: String,
    required: [true, "Username is required"],
    unique: true,
    trim: true,
    minlength: 3,
    maxlength: 15,
  },
  email: {
    type: String,
    required: [true, "Email is required"],
    unique: true,
  },
  role: {
    type: String,
    enum: ["user"],
    default: "user",
  },
  cards: [
    {
      type: mongoose.Schema.ObjectId,
      ref: "Card"
    },
  ],

[就在“卡片”所在的位置,我想在用户字段中包含保存用户ID的帖子的ID。这是Post模型:

    user: String,
  text: {
    type: String,
  },
  reward: {
    ...
  },
  user: {
    type: mongoose.Schema.ObjectId,
    ref: 'User',
    required: true
  },

这是将帖子保存到数据库的控制器:

exports.addCard = asyncHandler(async (req, res, next) => {
    //Add user to req.body
    req.body.user = req.user.id;

    const card = await Card.create(req.body);

    res.status(201).json({
        success: true, 
        message: 'Card created', 
        data: card
    });
});

[我想这应该在创建帖子时发生,就像在当前登录用户的用户模式下将帖子ID推送到帖子数组中一样?如何正确存档?

回答如下:

您可以进行第二次查询以插入_id

确保_id处的$match是类似于ObjectId("5d345234daa45")而不是5d345234daa45的ObjectId

exports.addCard = asyncHandler(async (req, res, next) => {
    //Add user to req.body
    req.body.user = req.user.id;

    const card = await Card.create(req.body);

    const id = card._id; // this should be your _id,  check with console.log() if this is your _id

    //dont forget to import your model 
    await User.aggregate([
       {
          $match: {
             _id: mongoose.Types.ObjectId(req.body.user)
          }
       },
       {
          $push: {
             cards: id
          }
       }
    ]);


    res.status(201).json({
        success: true, 
        message: 'Card created', 
        data: card
    });
});
发布评论

评论列表 (0)

  1. 暂无评论