我正在嘗試更新我的第二個模式,該模式在第一個模式中具有參考價值
ownerSchema js
我正在嘗試更新我的第二個模式。
ownerSchema.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,
位置。String,
startDate : Date。
endDate : Date, endDate : Date.
});
var Shop = mongoose.model('Shop', shopSchema) 。
所以我試影像這樣更新我的模式
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 }; {span class="hljs-attr">runValidators: 啟用。
);
};
我正在嘗試更新商店,但它不作業,我在哪里錯了,我不知道
。uj5u.com熱心網友回復:
你應該運行多次更新
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;
let newOwmer = new Owner.findOneAndUpdate(
{ ownerId: id },
{
$set: update,
},
{ runValidators: true ,lean:true}.
);
//得到回傳的doc并得到`shopPlace'并運行另一個查詢。
let newShop = new Shop.findOneAndUpdate(
{ _id: id },
{
$set: updateShop,
},
{ runValidators: true ,lean:true}.
);
};
uj5u.com熱心網友回復:
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。
//從Owner中獲取shopPlace _id。
const { shopPlace } = await Owner.findOneAndUpdate(
{ ownerId: id },
{
$set: update,
},
{ new:true}, new:true}.
);
//更新商店
let newShop = await Shop.findOneAndUpdate(
{ _id: shopPlace },
{
$set: updateShop,
},
{new:true}, {new:true}.
);
};
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/307935.html
標籤:
下一篇:Mongoose--按分組和填充。獲得一個人曾經交談過的獨特的用戶串列,就像Whatsapp的第一個螢屏,你有所有以前的用戶。
