【问题标题】:Mongoose : populate after saveMongoose:保存后填充
【发布时间】:2023-03-14 01:57:03
【问题描述】:

我有一个项目,我正在使用 MERN 堆栈以及 Redux

当我使用 get 请求获取普通数据时,在干净的页面刷新后一切正常,因为我可以使用 引用数据 填充查询,但是当我保存例如新数据,在response json 我只收到ObjectID 的原始数据,而不是它的引用数据。

我相信我需要使用新查询或类似于 .populate 的内容填充保存的响应,以便获取引用的数据,但我不知道这样做。

我已经尝试了一些我在这里读过的东西,例如save() 上的execPopulate,但没有奏效。

使用post 请求响应 JSON 示例:

{
  "suggestion": "61cb83b1e7a2d3469d2adc9e",
  "user": "61b4a9253f40b9c22969d11f",
  "content": "testing content",
  "rating": 3,
  "spoiler": false,
  "_id": "61d7df8c5883e282879c6fb9",
  "date": "2022-01-07T06:37:00.196Z",
  "upvotes": [],
  "__v": 0
}

响应示例与get 请求我也希望在save 中接收,请注意字段suggestionuser 已填充:

 {
    "_id": "61d7df8c5883e282879c6fb9",
    "suggestion": {
      "_id": "61cb83b1e7a2d3469d2adc9e",
      "title": "Beyond",
      "suggestion_type": "Book",
      "image": "91b2d138-883a-4fab-8e0c-a554dd466349.png",
      "token": "LSSRuE"
    },
    "user": {
      "_id": "61b4a9253f40b9c22969d11f",
      "username": "test_user",
      "avatar": "https://i.pinimg.com/2349087qsbjkqsbd.jpg"
    },
    "content": "Testing content",
    "rating": 3,
    "spoiler": false,
    "date": "2022-01-07T06:37:00.196Z",
    "__v": 0
  }

保存路线:

router.post(
  '/:token/reviews',
  [auth, [check('rating', 'Rating is required').not().isEmpty()]],
  async (req, res) => {
       
    try {
      const suggestion = await Suggestion.findOne({ token: req.params.token });
       
      const review = await Review.find({
        suggestion: suggestion.id,
      });          

      const newReview = new Review({
        content: req.body.content,
        rating: req.body.rating,
        spoiler: req.body.spoiler,
        user: req.user.id,
        suggestion: suggestion.id,
      });

      await newReview.save();

      res.json(newReview);
    } catch (error) {
      console.error(error.message);
      res.status(500).send('Server Error');
    }
  }
);

获取路线:

router.get('/:token/reviews/', auth, async (req, res) => {
  try {
    const suggestion = await Suggestion.findOne({
      token: req.params.token,
    });
    const review = await Review.find({
      suggestion: suggestion.id,
    })
      .populate('user', ['avatar', 'username', 'id'])
      .populate('suggestion', [
        'id',
        'token',
        'title',
        'image',
        'suggestion_type',
      ]);

    if (!review) {
      res
        .status(404)
        .json({ msg: 'No reviews found for the specified suggestion' });
    }

    res.json(review);
  } catch (error) {
    console.error(error.message);
    if (error.kind === 'ObjectId') {
      res.status(404).json({ msg: 'Review not found' });
    }
    res.status(500).send('Server Error');
  }
});

评论模型:

const mongoose = require('mongoose');

const ReviewSchema = new mongoose.Schema({
  suggestion: { type: mongoose.Schema.Types.ObjectId, ref: 'suggestion' },

  user: { type: mongoose.Schema.Types.ObjectId, ref: 'user' },

  date: { type: Date, default: Date.now },

  content: {
    type: String,
  },
  rating: {
    type: Number,
  },
  spoiler: { type: Boolean, default: 0 },

});

module.exports = mongoose.model('review', ReviewSchema);

【问题讨论】:

    标签: mongodb mongoose mongoose-populate


    【解决方案1】:

    解决了。

    我在await newReview.save(); 之后添加了这一行await newReview.populate('user');

    来自官方文档: https://mongoosejs.com/docs/populate.html#populate_an_existing_mongoose_document

    填充现有文档

    如果您有一个现有的 mongoose 文档并想要填充它的一些路径,您可以使用 Document#populate() 方法。

    const person = await Person.findOne({ name: 'Ian Fleming' });
    
    person.populated('stories'); // null
    
    // Call the `populate()` method on a document to populate a path.
    await person.populate('stories');
    
    person.populated('stories'); // Array of ObjectIds
    person.stories[0].name; // 'Casino Royale'
    

    【讨论】:

      猜你喜欢
      • 2021-11-17
      • 1970-01-01
      • 2019-07-04
      • 2012-11-11
      • 2019-04-23
      • 2014-06-19
      • 2012-01-22
      • 2012-07-03
      相关资源
      最近更新 更多