填充猫鼬模型未返回预期结果

发布于 2025-02-10 12:37:31 字数 2225 浏览 3 评论 0原文

我有两个型号用户和地方。两种模型都互相参考。我正在尝试填充该地点模型,以便当我删除位置时,该位置将从位置以及用户模型阵列中删除。位置模型从位置模型成功删除,但为什么没有从用户模型阵列中删除它。我在做什么错?

我的代码:

用于删除位置的模型


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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一花一树开 2025-02-17 12:37:31

更改创建者对象后,您需要保存它:

place.creator.places.pull(place);
await place.creator.save();

After changing the creator object, you need to save it:

place.creator.places.pull(place);
await place.creator.save();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文