satendra singh
satendra singh

Reputation: 187

how to get all data of on user in node js

I'm having a problem with node js. I haven't done much work on node js. The problem is that I have created a register form for registration and a PAN card form for taking PAN card details. The data of my PAN card has been displayed by all the users. And I am also able to display a PAN card data correctly, by PAN card id. But I want to display all the PAN cards of a user. So that every user can see all their PAN cards. Which that user has applied. Some details are given below

Please help me. How can i do this What logic can I apply with the gate method so that all the PAN cards applied by a user can be displayed correctly.

pan card apply code post method

// @route    POST pan/newpan
// @desc     Create a post
// @access   Private
router.post(
  '/newpan',
  [
    auth,
    [
      check('areaoffice', 'Area Office is required')
        .not()
        .isEmpty(),
      check('aocode', 'aocode is required')
        .not()
        .isEmpty(),
      check('ao', 'Ao is required')
        .not()
        .isEmpty(),
      check('range', 'Range is required')
        .not()
        .isEmpty(),
      check('aonumber', 'AO number is required')
        .not()
        .isEmpty(),
      check('application', 'Application Type is required')
        .not()
        .isEmpty(),
      check('old_pan', 'Old Pan Type is required')
        .not()
        .isEmpty(),
      check('category', 'Category Type is required')
        .not()
        .isEmpty(),
      check('applicant', 'Applicant Type is required')
        .not()
        .isEmpty(),
      check('firstname', 'First name is required')
        .not()
        .isEmpty(),
      check('middlename', 'Middle Name is required')
        .not()
        .isEmpty(),
      check('lastname', 'Last Name is required')
        .not()
        .isEmpty(),
      check('ffirstname', 'Father first Name is required')
        .not()
        .isEmpty(),
      check('fmiddlename', 'Father Middle Name is required')
        .not()
        .isEmpty(),
      check('flastname', 'Father Last Name is required')
        .not()
        .isEmpty(),
      check('mfirstname', 'Mother First Name is required')
        .not()
        .isEmpty(),
      check('mmiddlename', 'Mother Middle Name is required')
        .not()
        .isEmpty(),
      check('mlastname', 'Mother Last Name is required')
        .not()
        .isEmpty(),
      check('cardHolder', 'Card Holder Name is required')
        .not()
        .isEmpty(),
      check('dob', 'Date of Birth is required')
        .not()
        .isEmpty(),
      check('contect_number', 'Contact Number is required')
        .not()
        .isEmpty(),
      check('email', 'Email is required')
        .not()
        .isEmpty(),
      check('proofid', 'Id Proof is required')
        .not()
        .isEmpty(),
      check('proofadd', 'Id Address is required')
        .not()
        .isEmpty(),
      check('proofdob', 'Id Date of Birth is required')
        .not()
        .isEmpty(),
      check('gender', 'Gender is required')
        .not()
        .isEmpty(),
      check('adhar_number', 'Adhar Number is required')
        .not()
        .isEmpty(),
      check('address_f', 'Address is required')
        .not()
        .isEmpty(),
      check('address_v', 'Address is required')
        .not()
        .isEmpty(),
      check('address_p', 'Post office Address is required')
        .not()
        .isEmpty(),
      check('address_divi', 'Address of Division is required')
        .not()
        .isEmpty(),
      check('address_d', 'Address of Dist. is required')
        .not()
        .isEmpty(),
      check('state', 'State is required')
        .not()
        .isEmpty(),
      check('pin_code', 'Pin Code is required')
        .not()
        .isEmpty()
    ]
  ],
  async (req, res, next) => {
    const errors = validationResult(req);
    if (!errors.isEmpty()) {
      return res.status(400).json({ msg: errors.array() });
    }

    try {
      const user = await users.findById(req.user.id).select('-password');
      let image = req.files.image;
      let pdf = req.files.pdf;
      let sig = req.files.sig;
      const newPan = new Pan({
        areaoffice: req.body.areaoffice,
        aocode: req.body.aocode,
        ao: req.body.ao,
        range: req.body.range,
        aonumber: req.body.aonumber,
        application: req.body.application,
        old_pan: req.body.old_pan,
        category: req.body.category,
        applicant: req.body.applicant,
        firstname: req.body.firstname,
        middlename: req.body.middlename,
        lastname: req.body.lastname,
        ffirstname: req.body.ffirstname,
        fmiddlename: req.body.fmiddlename,
        flastname: req.body.flastname,
        mfirstname: req.body.mfirstname,
        mmiddlename: req.body.mmiddlename,
        mlastname: req.body.mlastname,
        cardHolder: req.body.cardHolder,
        dob: req.body.dob,
        contect_number: req.body.contect_number,
        email: req.body.email,
        proofid: req.body.proofid,
        proofadd: req.body.proofadd,
        proofdob: req.body.proofdob,
        gender: req.body.gender,
        adhar_number: req.body.adhar_number,
        address_f: req.body.address_f,
        address_v: req.body.address_v,
        address_p: req.body.address_p,
        address_divi: req.body.address_divi,
        address_d: req.body.address_d,
        state: req.body.state,
        pin_code: req.body.pin_code,
        image: image.name,
        pdf: pdf.name,
        sig: sig.name,
        imagepath: image.tempFilePath,
        username: user.username,
        avatar: user.avatar,
        user: req.user.id
      });

      image.mv(`./client/public/panImages/${image.name}`, function (err) {
        if (err) {
          return res.status(500).json({ msg: 'something Error' });
        }
      })
      sig.mv(`./client/public/panImages/${sig.name}`, function (err) {
        if (err) {
          return res.status(500).json({ msg: 'something Error' });
        }
      })
      pdf.mv(`./client/public/panImages/${pdf.name}`, function (err) {
        if (err) {
          return res.status(500).json({ msg: 'something Error' });
        }
      })
      const pan = await newPan.save();

      res.json(pan);
    } catch (err) {
      console.error(err.message);
      res.status(500).send('Server Error');
    }
  }
);

pan card result in mongoDB

  {
    "_id": "5eb81c00265d1a2404b2d5e6",
    "areaoffice": "JKHJHKJ",
    "aocode": "FADSF",
    "ao": "FDAFDF",
    "range": "ADSFAS",
    "aonumber": "JLK",
    "application": "KJLKJ",
    "old_pan": "KJLKJ",
    "category": "KJLKJ",
    "applicant": "KJLKJ",
    "firstname": "KJLJ",
    "middlename": "JJKHJK",
    "lastname": "JHJKHK",
    "ffirstname": "JHKJH",
    "fmiddlename": "JHJH",
    "flastname": "JHJHK",
    "mfirstname": "JHJKHK",
    "mmiddlename": "HKJHKJ",
    "mlastname": "KJKJHJKLH",
    "cardHolder": "SATENDRA SINGH",
    "dob": "HJKJHKJH",
    "contect_number": "HJKHLKJ",
    "email": "JHKJHK",
    "proofid": "JKHJHK",
    "proofadd": "JHJKHJ",
    "proofdob": "JHKJ",
    "gender": "HJKHJ",
    "adhar_number": "JHJKLH",
    "address_f": "JKHKJH",
    "address_v": "JKHKJH",
    "address_p": "JKHJK",
    "address_divi": "FG",
    "address_d": "HUHUJK",
    "state": "UIGHBJKJK",
    "pin_code": "JKHKJHJKJ",
    "image": "Festivals-and-Occasions.jpg",
    "pdf": "sample.pdf",
    "sig": "showcase.jpg",
    "username": "s",
    "user": "5eb2fa29d37d9a08741621c8",
    "date": "2020-05-10T15:21:36.675Z",
    "__v": 0
}

Get all pans Data method

// @route    GET pan/pans
// @desc     Get all pans
// @access   Private
router.get('/pans', adminAuth, async (req, res) => {
  try {
    const pans = await Pan.find().sort({ data: -1 });
    res.json(pans)
  } catch (error) {
    console.error(error.message)
    res.status(500).send('Server Error')
  }
});

get user profile data

{
    "_id": "5eb2fa29d37d9a08741621c8",
    "username": "s",
    "name": "s",
    "email": "[email protected]",
    "mobile": "s",
    "address": "s",
    "city": "s",
    "state": "s",
    "zipCode": "s",
    "date": "2020-05-06T17:55:53.001Z",
    "__v": 0
}

Upvotes: 0

Views: 349

Answers (2)

satendra singh
satendra singh

Reputation: 187

Thank you sir, this code is working. I just changed something in it.

router.get('/user/:user_id', auth, async (req, res) => {
try {
    if (req.params.user != "" && req.params.user != undefined) {
        return res.status(400).json({ msg: 'Payment not Found' })
    }
    const pans = await Pan.find({ user: req.user.id }).sort({ data: -1 });
    res.json(pans)
} catch (error) {
    console.error(error.message)
    res.status(500).send('Server Error')
}

});

Upvotes: 0

Puneet Singh
Puneet Singh

Reputation: 3543

To get pan details of a particular user you should get user_id in the route and then you can pass that user_id in Pan.find

router.get('/pans/:user_id', adminAuth, async (req, res) => {
  try {
    var query = {}
    if (req.params.user_id != "" && req.params.user_id != undefined {
     query.user_id = req.params.user_id
    }
    const pans = await Pan.find(query).sort({ data: -1 });
    res.json(pans)
  } catch (error) {
    console.error(error.message)
    res.status(500).send('Server Error')
  }
});

Upvotes: 1

Related Questions