填充猫鼬模型未返回预期结果
我有两个型号用户和地方。两种模型都互相参考。我正在尝试填充该地点模型,以便当我删除位置时,该位置将从位置以及用户模型阵列中删除。位置模型从位置模型成功删除,但为什么没有从用户模型阵列中删除它。我在做什么错?
我的代码:
用于删除位置的模型
const mongoose = require("mongoose");
const schema = mongoose.Schema;
const placeSchema = new schema({
title : {type :String , required :true} ,
description : {type :String , required :true} ,
image : {type :String , required :true} ,
address : {type :String , required :true} ,
location : {
lat: {type : Number , required : true} ,
lng : {type :Number , required : true}
} ,
creator : {type :mongoose.Types.ObjectId , required :true , ref :"User"}
});
module.exports = mongoose.model("Place" , placeSchema);
用户模型
const mongoose = require("mongoose");
const uniqueValidator = require("mongoose-unique-validator");
const schema = mongoose.Schema;
const userSchema = new schema({
userName : {type : String , required : true} ,
email : {type : String , required : true , unique:true} ,
password : {type : String , required : true , minlength:6} ,
image :{type : String , required : true} ,
places : [{type : mongoose.Types.ObjectId , required : true , ref : "Place"}]
});
userSchema.plugin(uniqueValidator);
module.exports = mongoose.model("User" , userSchema);
API,并从用户模型的阵列中删除位置
const deletePlace=async(req, res , next)=>{
const placeId = req.params.pid;
let place ;
try
{
place = await PLACE.findById(placeId).populate('creator');
}
catch(err)
{
const error = new Error("SOMETHING WENT WRONG , COULD NOT DELETE PLACE");
error.code = 500;
return next(error);
}
if(!place)
{
const error = new Error("NO PLACE FOUND WITH SUCH ID");
error.code = 500;
return next(error);
}
try
{
const session = await mongoose.startSession();
session.startTransaction();
await PLACE.deleteOne({placeId});
place.creator.places.pull(place);
}
catch(err)
{
const error = new Error("SOMETHING WENT WRONG , COULD NOT DELETE PLACE");
error.code = 500;
return next(error);
}
res.status(200).json({message: "PLACE DELETED"});
};
I have two models user and places . both models have refs to each other . I'm trying to populate the place model so that when I remove place , the place get removed from places and also from places array of user model . Place from Place model is removed successfully but why it is not getting removed from places array of user model . what am I doing wrong ?
My Code :
Place Model
const mongoose = require("mongoose");
const schema = mongoose.Schema;
const placeSchema = new schema({
title : {type :String , required :true} ,
description : {type :String , required :true} ,
image : {type :String , required :true} ,
address : {type :String , required :true} ,
location : {
lat: {type : Number , required : true} ,
lng : {type :Number , required : true}
} ,
creator : {type :mongoose.Types.ObjectId , required :true , ref :"User"}
});
module.exports = mongoose.model("Place" , placeSchema);
User Model
const mongoose = require("mongoose");
const uniqueValidator = require("mongoose-unique-validator");
const schema = mongoose.Schema;
const userSchema = new schema({
userName : {type : String , required : true} ,
email : {type : String , required : true , unique:true} ,
password : {type : String , required : true , minlength:6} ,
image :{type : String , required : true} ,
places : [{type : mongoose.Types.ObjectId , required : true , ref : "Place"}]
});
userSchema.plugin(uniqueValidator);
module.exports = mongoose.model("User" , userSchema);
api for deleting place and also deleting place from places array of the user model
const deletePlace=async(req, res , next)=>{
const placeId = req.params.pid;
let place ;
try
{
place = await PLACE.findById(placeId).populate('creator');
}
catch(err)
{
const error = new Error("SOMETHING WENT WRONG , COULD NOT DELETE PLACE");
error.code = 500;
return next(error);
}
if(!place)
{
const error = new Error("NO PLACE FOUND WITH SUCH ID");
error.code = 500;
return next(error);
}
try
{
const session = await mongoose.startSession();
session.startTransaction();
await PLACE.deleteOne({placeId});
place.creator.places.pull(place);
}
catch(err)
{
const error = new Error("SOMETHING WENT WRONG , COULD NOT DELETE PLACE");
error.code = 500;
return next(error);
}
res.status(200).json({message: "PLACE DELETED"});
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更改创建者对象后,您需要保存它:
After changing the creator object, you need to save it: