How do I delete a entry "object" from a mongoose user model schema

I've figured out how to make a put request to store a pdf to my "User" schema

how would I access the coverLetter object from the "req.profile = user" to make a delete request of only this entry in this users profile.

I've only managed figured out how to delete the entire user or document from the DB.

front-end: react

backend: node.js, express, mongoDB, mongoose

thanks for any suggestions


**Back-End**


router.delete("/user/documents/coverLetter/:userId", deleteData); 
router.param("userId", userById);




exports.userById = (req, res, next, id) => {
  User.findById(id).exec((err, user) => {
    if (err || !user) {
      return res.status(400).json({
        error: "User not found",
      });
    }
    req.profile = user; // adds profile object in req with user info
    next();
  });
};



//profile object


}
{
  coverLetter: {
    contentType: 'application/pdf',
    data: Binary {
      _bsontype: 'Binary',
      sub_type: 0,
      position: 396412,
      buffer: <Buffer 25 50 44 46 2d 31 2e 33 0a 25 c4 e5 f2 e5 eb a7 f3 a0 d0 c4 c6 0a 34 20 30 20 6f 62 6a 0a 3c 3c 20 2f 4c 65 6e 67 74 68 20 35 20 30 20 52 20 2f 46 69 ... 396362 more bytes>
    }
  },
  _id: 60c2ef9e13f13d902a07ab0b,
  fName: 'gh',
  lName: 'h',
  email: 'b@gmail.com',
  salt: '73bb4607-47bf-407f-9155-0b65d19c21be',
  hashed_password: 'a3e08880687ec89344e2a5c6989b1f68bcceee1b',
  created: 2021-06-11T05:07:42.472Z,
  __v: 0,
  updated: 2021-06-11T05:08:01.099Z
}```


// need help with this block


exports.deleteData = (req, res) => {
let user = req.profile

// how do i access the coverLetter to delete it

}






const userSchema = new mongoose.Schema({

    fName: {
        type: String,
        trim: true,
        required: true
    },
    lName: {
        type: String,
        trim: true,
        required: true

},
    
    coverLetter: {
        data: Buffer,
        contentType: String,
    },
   
    
});

module.exports = mongoose.model("User", userSchema);





**front-end code**

export const deleteCover = (userId, token) => {
    return fetch(`${process.env.REACT_APP_API_URL}/user/documents/coverLetter/${userId}`, {
        method: "DELETE",
        headers: {
            Accept: "application/json",
            Authorization: `Bearer ${token}`
        }
    })
        .then(response => {
            return response.json();
        })
        .catch(err => console.log(err));
};



Read more here: https://stackoverflow.com/questions/67931759/how-do-i-delete-a-entry-object-from-a-mongoose-user-model-schema

Content Attribution

This content was originally published by Brendan at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: