Home > Blockchain >  how to delete a particular photo object of the user who is requesting if that photo object has a pho
how to delete a particular photo object of the user who is requesting if that photo object has a pho

Time:01-31

how to delete a particular photo object of the user who is requesting if that photo object has a photo starting with file://? let me introduce you to what I am adding image feature in my app I created a NodeJs route for it that handles all that and that route is working fine as expected now I am facing a problem that I am using the expo-camera package and that package stores the image in the user cache first then that image is converted into firebase URL

So when I upload the image in then it creates two objects at the same time for the same image in Mongodb one object contains that cached image which starts from file:// and 2nd object starts with the firebase URL which is fine but how can I create a function in Nodejs which deletes that object which that object starts with file://? how can I do this in Monodb? I am getting that user email who is requesting to upload the image

You can check my code on how I tried to do that and it logs Result : [object Object] can it not deletes and save the user how can I do things properly that work? you can see the images that I attached here I have a photos array that contains objects and those objects contain a string called photo which contains that file:// URL

How it was looking currenlty with file:// uri

How it should look like after delete process of file://

What i tried:

const deletePhoto = async (email) => {
    try {
      const result = await User.updateOne({
        email: email,
      }, {
        $pull: {
          photos: {
            photo: {
              $regex: "^file:"
            }
          }
        }
      });
  
      console.log(`Result : ${result}`);
      return result;
    } catch (error) {
      console.error(error);
      return error;
    }
  };

CodePudding user response:

Try with:

const result = await User.findOneAndUpdate(
  {
    email: email,
  },
  {
    $pull: {
      'photos.photo': {
        $regex: /^file:/,
      },
    },
  },
  { new: true }
);
  • Related