Estoy tratando de actualizar mi segundo esquema que tiene referencia en el primer esquema
propietarioEsquema.js
var ownerSchema = Schema({ fname : String, lname : String, shopPlace : { type: Schema.Types.ObjectId, ref: 'Shop' } }); var Owner = mongoose.model('Owner', ownerSchema);shopSchema.js
var shopSchema = Schema({ shopName : String, location : String, startDate : Date, endDate : Date }); var Shop = mongoose.model('Shop', shopSchema);así que estoy tratando de actualizar mi esquema de esta manera
const update = async (req, res) => { const { id } = req.params; let update = {}; if (req.body.fname) update.fname = req.body.fname; if (req.body.lname) update.lname = req.body.lname; if (req.body.shopPlace.shopName) update.shopPlace.shopName = req.body.shopPlace.shopName; if (req.body.shopPlace.location) update.shopPlace.location = req.body.shopPlace.location; let newOwmer = new Owner.updateOne( { ownerId: id }, { $set: update, }, { runValidators: true } ); };Estoy tratando de actualizar la tienda pero no funciona y ¿dónde me equivoco? No lo sé.
const update = async (req, res) => { const { id } = req.params; let update = {}; let updateShop = {} if (req.body.fname) update.fname = req.body.fname; if (req.body.lname) update.lname = req.body.lname; if (req.body.shopPlace.shopName) updateShop.shopPlace.shopName = req.body.shopPlace.shopName; if (req.body.shopPlace.location) updateShop.shopPlace.location = req.body.shopPlace.location; // get shopPlace _id from Owner const { shopPlace } = await Owner.findOneAndUpdate( { _id: id }, { $set: update, }, { new:true} ); // here you must have the document you just updated. Just recover the id of the shop to modify it in turn // update shop let newShop = await Shop.findOneAndUpdate( { _id: shopPlace }, { $set: updateShop, }, {new:true} ); };