Query mongoDB find() -


this question has answer here:

i'm new mongodb, want grab "posts" 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 1                         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 empty array guess statement must respect both createdby , to.

i found answer

post.find({$or : [                         { createdby:user.username},                         { to: user.username }                     ]}, 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -