【问题标题】:Express JS How to push object to an array of child objectExpress JS如何将对象推送到子对象数组
【发布时间】:2022-01-11 22:49:25
【问题描述】:

我正在处理学校目录,我想将对象推送到我的位置数组,这是我的子对象 Floor 的属性,它属于名为 Map 的父对象。

楼层架构

const floorSchema = new mongoose.Schema({
    title:{
      type:String,
      required:true
    },
    file:{
      type:String,
      required:true
    },
    floorSlug:{
      type:String,
      required:true,
    },
    locations:[{
        locationTitle: String,
        locationSlug: String,
        description: String,
        position_x: String,
        position_y: String,
        position_z: String,
    }],
})

我添加位置对象的路线是这样的,我试图“编辑地板”,特别是试图推送到位置属性:

router.patch('/:slug/:floorSlug/add-location', (req, res) => {
  const slug = req.params.slug;
  const { floorSlug } = req.body;
  const querySlug = '^' + slug + '$';
  const locations = req.body.locations;
  Map.findOneAndUpdate(
      {
        $and: [
          { "slug": { '$regex': querySlug, $options: 'i' } },
          { 'floorSlug': floorSlug }]
      },
      {
        $push: { "locations": locations }
      }
  )
  .then(map => {
    map.floors.push({
      locations
    })
    map.save()
      .then(map => res.json(map))
      .catch(err => res.status(400).json('Error: ' + err));
  })
  .catch(err => res.status(400).json('Error: ' + err))

});

我的预期结果应该是这样的:

    {
    "map": [
        {
            "floors": [
                {
                    "title": "upper level",
                    "file": "mapurl.com",
                    "floorSlug": "upper-level"
                    "locations": [
                        {
                            "locationTitle": "title",
                            "locationSlug": "title-1",
                            "description": "description",
                            "position_x": "1",
                            "position_y": "1",
                            "position_z": "1"
                        }
                    ]
                },
                {
                    "title": "lower level",
                    "file": "mapurl.com",
                    "floorSlug": "lower-level",
                    "locations": [
                        {
                            "locationTitle": "title",
                            "locationSlug": "title-1",
                            "description": "description",
                            "position_x": "1",
                            "position_y": "1",
                            "position_z": "1"
                        }
                        {
                            "locationTitle": "title",
                            "locationSlug": "title-2",
                            "description": "description",
                            "position_x": "2",
                            "position_y": "2",
                            "position_z": "2"
                        }
                    ]
                }
            ],
            "_id": "61cfb0911dfb0970a0eb00cb",
            "title": "map",
            "slug": "map-1",
        }
    ]
}

感谢您的帮助!

【问题讨论】:

  • Array.prototype.push 将元素作为参数添加到数组中 - 看起来您将回调函数作为参数传递,这是不正确的。

标签: javascript arrays mongodb express mongoose


【解决方案1】:

您可以使用$push 来完成。

我认为这段代码可能会对你有所帮助!

router.patch('/:slug/:floorSlug/add-location', (req, res) => {
  const slug = req.params.slug;
  const { floorSlug } = req.body;
  const querySlug = '^' + slug + '$';
  const locations = req.body.locations;
  Map.findOneAndUpdate(
    {
      $and: [
        { "slug": { '$regex': querySlug, $options: 'i' } },
        { 'floors.floorSlug': floorSlug }]
    },
    {
      $push: { 'floors.$.locations': locations }
    }
  );
});

【讨论】:

  • 非常感谢,这帮助我找到了位置属性,但我想将一个对象推送到位置数组。我该如何定义?
  • 使用$push可以将对象推入locations数组!
  • 非常感谢,我已经更新了我的代码,我想知道我是否正确保存?
  • @AlbertoRamos 我更新了我的答案!如果我的回答能帮助你接受!
  • 每次我发出补丁请求时,它仍然会推送一个新的新对象。你能告诉我如何正确保存它吗?请检查我的更新代码
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-04-08
  • 1970-01-01
  • 2017-08-03
  • 1970-01-01
  • 1970-01-01
  • 2017-06-05
相关资源
最近更新 更多