Attention needed! Likes and comments temporary fix.
littledivy opened this issue · 3 comments
littledivy commented
Hello
I had created this repo months ago and at that time I had to create a temporary fix of the following things:-
- Liking a post -
// Under utils/handlers/user.js
function like(user, like, _id, cb) {
User
.findOne(user)
.exec((err, obj) => {
// if (!obj) return cb("Does not exist.",null);
//console.log(obj);
for(var i=0;i<obj.posts.length;i++) {
if(obj.posts[i]._id == _id) {
obj.posts[i].likes.push(like.by);
obj.delete(); // Need to fix this
var newUser = new User({
username:obj.username,
password:obj.password,
firstname:obj.fn,
lastname:obj.ln,
dob:obj.dob,
bio:obj.bio,
profile_pic:obj.profile_pic,
posts:obj.posts,
followers:obj.followers,
likes:obj.likes,
lastLogin:new Date(),
});
newUser.save((err, res) => {
cb(err, true);
})
}
}
})
}
In function like(),
I couldn't save the document after pushing the like into the post array of a user.
I tried
obj.posts[i].likes.push(like.by);
// and then simply did
obj.save(() => {});
This is not working...and not saving the updated document.
- Comments -
Same issue as above
What did I do as a temporary fix.
I pushed the like into post array and then deleted the original doc and then created a new doc with updated like/comment.
Regards,
Divy Srivastava
dan-online commented
Ill look into this
dan-online commented
Now works using this code
obj.posts[i].likes.push(like.by); obj = new User(obj); obj.save((err) => {cb(err, true)})
littledivy commented
Thanks!
Issue resolved
Achievement Unlocked! lol