0

The attraction model is already created, I am just trying to add the facility Id to it after a new facility is added.The facility successfully adds the attraction Id to it but the attraction does not seem to update to add the facility id. Code below is where the new facility is created.

const facility = new Facility({ paidWifi, freeWifi , cashMachines ,lockers , disabledToilets , electricCarParking ,attraction:attractionId });
      facility.save(function (err) {
        if (err) return handleError(err);
        // thats it!
      });
      const attraction =  Attraction.findOneByIdAndUpdate(attractionId, { facility });
      console.log(attraction);
      return facility;
Coder1234
  • 389
  • 1
  • 3
  • 9
  • Try adding the `findOneByIdAndUpdate` into the `save` callback. You need access to the `_id` that's created during save. – lux Feb 18 '19 at 21:20
  • facility.save(function (err) { if (err) return handleError(err); const attraction = Attraction.findByIdAndUpdate({attractionId}, { facility }); console.log(attraction); }); like this ? – Coder1234 Feb 18 '19 at 21:30
  • Use the document it provides you in the callback. Right now, you're only getting `err` out of the arguments. See here: https://mongoosejs.com/docs/api.html#model_Model-save `facility.save(function (err, newFacility) {...//do update here with newFacility}` – lux Feb 18 '19 at 23:06

0 Answers0