Javascript Mongoose populate返回空数组或ObjectID列表

Javascript Mongoose populate返回空数组或ObjectID列表,javascript,node.js,express,mongoose,mongoose-schema,Javascript,Node.js,Express,Mongoose,Mongoose Schema,我正在通过构建关系API来练习我的express.js技能,并且正在努力填充模式中的键 我正在建造它,所以我有一个财产清单,这些财产有单位。这些单元有一个propertyId键 这当前返回的是一个空数组,而如果删除populate({}),它将返回一个objectid数组 我读过很多帖子,有些人用.populate({path:'path',model:model})解决了这个问题;但这似乎并不奏效。我想这可能是我在单元中添加propertyId的方式,但我不确定。谁能看出我错在哪里?任何帮助都

我正在通过构建关系API来练习我的express.js技能,并且正在努力填充模式中的键

我正在建造它,所以我有一个财产清单,这些财产有单位。这些单元有一个propertyId键

这当前返回的是一个空数组,而如果删除populate({}),它将返回一个objectid数组

我读过很多帖子,有些人用.populate({path:'path',model:model})解决了这个问题;但这似乎并不奏效。我想这可能是我在单元中添加propertyId的方式,但我不确定。谁能看出我错在哪里?任何帮助都将不胜感激

下面是模式

财产:

const mongoose = require('mongoose');
const { Schema } = mongoose;

const PropertySchema = new Schema({
  title: {
    type: String,
    required: true
  },
  date: {
    type: Date,
    default: Date.now
  },
  units: [
    {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'unit'
    }
  ]
});

module.exports = Property = mongoose.model('property', PropertySchema);
单位:

然后我创建了如下单元:

-- api/properties/:id/units --
router.post('/:id/units', async (req, res) => {

  // Get fields from req.body
  const { title } = req.body;

  // Get current property
  const property = await Property.findById(req.params.id);

  try {

    // Throw error if no property
    if (!property) {
      return res.status(400).json({ msg: 'Property not found' });
    }

    // Create new unit
    const newUnit = new Unit({
      title,
      propertyId: req.params.id
    });

    // Add new unit to property's units array
    property.units.unshift(newUnit);

    // Save property
    await property.save();

    // Return successful response
    return res.status(200).json(property);

  } catch (error) {
    console.error(error.message);
    return res.status(500).send('Server error');
  }
});
[
    "5ff7256cda2f5bfc1d2b9108",
    "5ff72507acf9b6fb89f0fa4e",
    "5ff724e41393c7fb5a667dc8",
    "5ff721f35c73daf6d0cb5eff",
    "5ff721eb5c73daf6d0cb5efe",
    "5ff7215332d302f5ffa67413"
]
并尝试填充GET请求

-- /api/properties/:id/units --
const Unit = require('../../models/Unit');

router.get('/:id/units', async (req, res) => {
  const property = await Property.findOne({ _id: req.params.id }).populate({path: 'units', model: Unit});
  const propertyUnits = property.units;
  return res.status(200).json(propertyUnits);
});
如果删除.populate({path:'units',model:Unit});,我得到一个单位id列表,如下所示:

-- api/properties/:id/units --
router.post('/:id/units', async (req, res) => {

  // Get fields from req.body
  const { title } = req.body;

  // Get current property
  const property = await Property.findById(req.params.id);

  try {

    // Throw error if no property
    if (!property) {
      return res.status(400).json({ msg: 'Property not found' });
    }

    // Create new unit
    const newUnit = new Unit({
      title,
      propertyId: req.params.id
    });

    // Add new unit to property's units array
    property.units.unshift(newUnit);

    // Save property
    await property.save();

    // Return successful response
    return res.status(200).json(property);

  } catch (error) {
    console.error(error.message);
    return res.status(500).send('Server error');
  }
});
[
    "5ff7256cda2f5bfc1d2b9108",
    "5ff72507acf9b6fb89f0fa4e",
    "5ff724e41393c7fb5a667dc8",
    "5ff721f35c73daf6d0cb5eff",
    "5ff721eb5c73daf6d0cb5efe",
    "5ff7215332d302f5ffa67413"
]

我不知道,你为什么不这样尝试:

-- api/properties/:id/units --
router.post('/:id/units', async (req, res) => {

  // Get fields from req.body
  const { title } = req.body;

  // Get current property
  const property = await Property.findById(req.params.id);

  try {

    // Throw error if no property
    if (!property) {
      return res.status(400).json({ msg: 'Property not found' });
    }

    // Create new unit
    const newUnit = new Unit({
      title,
      propertyId: req.params.id
    });

    // Add new unit to property's units array
    property.units.unshift(newUnit);

    // Save property
    await property.save();

    // Return successful response
    return res.status(200).json(property);

  } catch (error) {
    console.error(error.message);
    return res.status(500).send('Server error');
  }
});
[
    "5ff7256cda2f5bfc1d2b9108",
    "5ff72507acf9b6fb89f0fa4e",
    "5ff724e41393c7fb5a667dc8",
    "5ff721f35c73daf6d0cb5eff",
    "5ff721eb5c73daf6d0cb5efe",
    "5ff7215332d302f5ffa67413"
]
wait Property.findOne({u id:req.params.id})。填充('units'))
我一直在尝试上面的代码,它的工作

注意:确保检查您的req.params.id不为null或未定义,并确保您在mongodb中找到的数据不为空


更新:我一直在尝试你的代码,它工作得很好。

我不知道,你为什么不这样尝试:

-- api/properties/:id/units --
router.post('/:id/units', async (req, res) => {

  // Get fields from req.body
  const { title } = req.body;

  // Get current property
  const property = await Property.findById(req.params.id);

  try {

    // Throw error if no property
    if (!property) {
      return res.status(400).json({ msg: 'Property not found' });
    }

    // Create new unit
    const newUnit = new Unit({
      title,
      propertyId: req.params.id
    });

    // Add new unit to property's units array
    property.units.unshift(newUnit);

    // Save property
    await property.save();

    // Return successful response
    return res.status(200).json(property);

  } catch (error) {
    console.error(error.message);
    return res.status(500).send('Server error');
  }
});
[
    "5ff7256cda2f5bfc1d2b9108",
    "5ff72507acf9b6fb89f0fa4e",
    "5ff724e41393c7fb5a667dc8",
    "5ff721f35c73daf6d0cb5eff",
    "5ff721eb5c73daf6d0cb5efe",
    "5ff7215332d302f5ffa67413"
]
wait Property.findOne({u id:req.params.id})。填充('units'))
我一直在尝试上面的代码,它的工作

注意:确保检查您的req.params.id不为null或未定义,并确保您在mongodb中找到的数据不为空


更新:我一直在尝试您的代码,它工作正常。

该问题是由命名不一致、未保存新创建的单元以及更新的属性引起的


我仔细检查了我所有的模式导出和引用,注意到我在某些情况下使用大写,在其他情况下使用小写,并在POST请求中保存了newUnit和更新的属性,结果成功了。

该问题是由命名不一致以及未保存新创建的单元和更新的属性引起的


我仔细检查了我所有的模式导出和引用,注意到我在某些实例中使用大写,在其他实例中使用小写,并在POST请求中保存了newUnit以及更新的属性,它成功了。

如果我只使用.populate('units'),它将返回一个空数组。其工作原理是将单位保存到属性,但不使用单位对象填充属性。在我的数据库中,我目前只有一个objectid数组。没错,它仍然在工作。如果你需要截图,我可以添加它。它仍然只是为我显示ObjectId。可以添加一个屏幕截图来显示属性中的单位对象吗?如果我只使用.populate('units'),它将返回一个空数组。其工作原理是将单位保存到属性,但不使用单位对象填充属性。在我的数据库中,我目前只有一个objectid数组。没错,它仍然在工作。如果你需要截图,我可以添加它。它仍然只是为我显示ObjectId。能否添加一个屏幕截图,显示物业内的单位对象?