I am trying to add a bookmark feature to my site so that the user can save the posts he wants to save, but it does not work as expected
It is assumed that when the user clicks on the bookmark button on the site, his ID is stored in the saves array, and then when the user tries to view the saved posts, every post that includes the ID of this user in the saves array is called.
When I try to fetch data from the front end I get this error
"Cannot read properties of undefined (reading ‘includes’)"
Note: that saves is defined as an array of users id’s in schema
this is my controller.js function:
const getSavedPosts = async(req, res) => {
try {
const userId = req.user._id;
const post = await Post.find();
const savedP = await post.saves.includes(userId);
if(savedP){
const userSavedPost = await Post.find({userId: {$in: savedP} })
res.status(200).json(userSavedPost);
console.log(userSavedPost)
} else {
return;
}
} catch (err) {
res.status(500).json({ error: err.message });
}
};
PostModel.js
import mongoose from "mongoose";
const postSchema = mongoose.Schema({
postedBy: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
required: true
},
text: {
type: String,
maxLength: 500
},
img: {
type: String,
},
likes: {
// array of users id's
type: [mongoose.Schema.Types.ObjectId],
ref: "User",
default: []
},
saves: {
// array of users id's
type: [mongoose.Schema.Types.ObjectId],
ref: "User",
default: []
},
replies: [
{
userId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
required: true
},
text: {
type: String,
required: true
},
userProfilePic: {
type: String,
},
username: {
type: String
}
}
]
}, {timestamps: true}
)
const Post = mongoose.model('Post', postSchema);
export default Post;
2
Answers
Isn’t
post
coming back as an array?Then maybe you’d like to loop through each element?
Something like:
const savedP = post.filter((singlePost) => singlePost.saves.includes(userId));
You have made a good start but there are quite a few issues with your code. Therefore I will just give you a solution with some notes to explain them as it’s easier that way.
Update your
postSchema
so that thelikes
andsaves
properties are like so:Update your
getSavedPosts
function like so: