Arrays Mongoose FindByiandUpdate:can';不更新数组字段

Arrays Mongoose FindByiandUpdate:can';不更新数组字段,arrays,mongodb,mongoose,Arrays,Mongodb,Mongoose,我是刚接触猫鼬的,我有个问题。 在我的应用程序中,我有这样一个旅行模型: const travelSchema = new mongoose.Schema({ title: { type: String, required: [true, 'Please add a title'], trim: true, maxlength: [50, 'Title can not be more than 50 characters'], }, cities: [

我是刚接触猫鼬的,我有个问题。 在我的应用程序中,我有这样一个旅行模型:

const travelSchema = new mongoose.Schema({
  title: {
    type: String,
    required: [true, 'Please add a title'],
    trim: true,
    maxlength: [50, 'Title can not be more than 50 characters'],
  },
  cities: [
    {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'City',
    },
  ],
});
还有这样的城市模型:

const citySchema = new mongoose.Schema({
  name: {
    type: String,
    required: true,
    trim: true,
  },
  location: {
    type: {
      type: String,
      enum: ['Point'],
      required: true,
    },
    coordinates: {
      type: [Number],
      required: true,
    },
  },
  travels: [
    {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'Travel',
    },
  ],
});
exports.deleteTravel = asyncHandler(async (req, res, next) => {
  const travel = await Travel.findByIdAndDelete(req.params.id);
  travel.cities.map(async cityId => {
    await City.findByIdAndUpdate(
      cityId,
      { $pull: { travels: travel._id } },
      {
        new: true,
        runValidators: true,
      }
    );
  });

因此,当我删除一次旅行时,我想从与旅行相关的城市的“旅行”字段中删除旅行id

我在这里:

exports.deleteTravel = asyncHandler(async (req, res, next) => {
  const travel = await Travel.findByIdAndDelete(req.params.id);
  travel.cities.map(cityId => {
    City.findByIdAndUpdate(
      cityId,
      { travels: travels.filter(id => id !== travel._id) },
      {
        new: true,
        runValidators: true,
      }
    );
  });


  res.status(200).json({ success: true, data: {} });
});
我收到这个错误消息:错误:未定义旅行

你知道为什么吗

非常感谢

它是这样工作的:)