I just need some advice here, i cant seem to delete an item thats in my nested array in my User model (user.likedSongs[]).
React button to delete function:
const handleDelete = async (e, index) => {
const trackToDelete = user.data.likedSongs[index]._id; // gets the right track
let res = await songService.removelikeFunction(
trackToDelete,
user.data._id
);
console.log(res);
// setMessage(res.data.message);
};
React service function:
const removelikeFunction = async (trackToDelete, id) => {
try {
const res = await axios.delete(
`${API_SONGS_URL}/like/${id}`,
trackToDelete,
config
);
console.log(res);
return res;
} catch (error) {
if (error.response) {
return error.response.data;
}
}
};
Node/Mongoose API function - reciving the song here seems to get me an Object Id thats different all the time?
async deleteLike(req, res) {
const user = await User.findById(req.params.id);
const song = await Song(req.body.id);
try {
// user.likedSongs.deleteOne({ _id: req.body._id });
const result = await user.likedSongs.deleteOne(song);
res.status(200).json({
success: result.acknowledged,
data: result,
});
await user.save();
} catch (error) {
if (error) {
res.status(400).send({ message: 'Like could not be deleted' });
}
}
},