【问题标题】:Mongoose: Populate Vehicle array on UsersMongoose:在用户上填充车辆数组
【发布时间】:2019-03-28 09:07:50
【问题描述】:

好的,这里有一些我不理解的基本内容。 我希望能够从车辆模型中获取最大范围,但我似乎无法填充工作......如果我输入 owner.vehicles[0].make 或其他任何未定义的内容。这是我的代码。需要明确的是,是的,用户正在获取额外的车辆 ID,但我无法访问任何车辆信息,因此它无用

  let owner = await User.findOne({_id: req.user._id});
        console.log(owner);
        owner.vehicles.push(newVehicle);
        owner = await owner.save();

         owner = await User.findOne({ _id: req.user._id }).populate('vehicles');
         owner = await owner.save();

【问题讨论】:

    标签: javascript json mongoose


    【解决方案1】:

    根据Document.prototype.populate(),您要么必须将回调作为第二个参数传递给populate(),要么调用execPopulate()

     owner = await User.findOne({ _id: req.user._id }).populate('vehicles').execPopulate(); 
    

    【讨论】:

      猜你喜欢
      • 2021-02-23
      • 2019-07-28
      • 2019-01-31
      • 2014-12-05
      • 2019-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多