0

I'm new to MongoDB, i want to grab all the "posts" that fits createdBy OR to.

router.get('/getPublicProfilePosts/:username',(req,res)=>{
    if(!req.params.username){
        res.json({success:false,message:"No username provided"});
    }
    else{
        User.findOne({username:req.params.username},(err,user)=>{
            if(err){
                res.json({success:false,message:'Something went wrong '+err});
            }
            else{
                if (!user) {
                    res.json({success:false,message:'User not found'});
                }
                else{
                    Post.find({createdBy:user.username, to:user.username},(err,posts)=>{ //this one
                        if(err){
                            res.json({success:false,message:'Something went wrong '+err});
                        }
                        else{
                            if (!posts) {
                                res.json({success:false,message:'Posts not found !'});
                            }
                            else{
                                res.json({success:true,posts:posts});
                            }
                        }
                    })
                }
            }
        });
    }
});

Post.find({createdBy:user.username, to:user.username} returns an empty array so i guess the statement must respect BOTH createdBy and to.

Community
  • 1
  • 1
Azoulay Jason
  • 2,787
  • 5
  • 21
  • 46

1 Answers1

0

I just found the answer

Post.find({$or : [
                        { createdBy:user.username},
                        { to: user.username }
                    ]},
Azoulay Jason
  • 2,787
  • 5
  • 21
  • 46