recursive calls in Javascript API (Promises)

Viewed 53

I am trying to fetch data from one MongoDB collection and with that result, I am passing that value into another function to get the data from GridFS files for same id.

router.get('/employee/:id', async(req,res) =>{
    let gID = req.params.id;
    var getUser = await User.find({id:gID});
    //console.log(res);
    
    console.log(getUser);
    if(getUser){
        try{
            
            gfs.files.find({objectID:getUser.id}).toArray(async (err, files) => {
                if (!files || files.length === 0) {
                    res.send({
                        message: "No User Found",
                      });
                } else {
                    
                    res.send({
                        message: "data fetched",
                        data: getUser,
                        image: files
                      });
                }
            });
        }catch(err){
            res.send({
                message:err.code,
                data:err
            });
        }
    }else{
        res.send({
            message:"error in getting the data",
        });
    }
    
})

I tried of this but I am not able to get the expected result, I am getting the user information from getUser but I am not able to fetch the file tagged with that user from GridFS. Can anyone tell me where I am lagging and how could I correct this.

0 Answers
Related