0

In my route I am trying to get the registers with a dynamic columns, than the client request the route like a "router.get('/get-by/'" than specify the field of my table...

I'm using like this

var field = req.body.field
    Territory
      .findOne({
        where: {
          field : req.params.id
        }
      })

but nothing works. Can anyone help me?

theduck
  • 2,589
  • 13
  • 17
  • 23
  • Could you please provide information about the specific errors you're encountering. This will help in the debugging process. – jhill515 Jan 09 '20 at 18:29

2 Answers2

0

Try this. [field]

var field = req.body.field
Territory
  .findOne({
    where: {
      [field] : req.params.id
    }
  })

I'm using like this:

var column = dynamicTable[0]
var value = dynamicTable[1]  
HasReports.create({
    ReportId: req.body.id,
    [column]: value, // Changable for report type..
    ReportedById: req.user.id,
    reportHasTitle: req.body.reportHasTitle,
    reportDescription: req.body.description,
    reportedByIp: ip
})
mehmetdemiray
  • 976
  • 4
  • 13
  • 32
0

im sloved with this :

    router.get('/get-by/',
passport.authenticate('jwt',{ session: false}),
[
  query('field').not().isEmpty().withMessage('territory field is empty'),
  query('value').not().isEmpty().withMessage('territory value is empty'),
],
function(req, res) {
  var token = getToken(req.headers);
  if (token) {
    const errors = validationResult(req);
    if (!errors.isEmpty()) {
      return res.status(422).json({ errors: errors.array() });
    }
    let filters = {}
    filters[req.query.field] = req.query.value;

    Territory.
      findAll({
        where: filters
      })
      .then((territory) => {
        if (!territory) {
          return res.status(401).send({
            message: 'Territory not found.',
          });
        }
        res.json({ success: true, territory: territory });
      })
      .catch((error) => res.status(400).send(error));
  } else {
    return res.status(403).send({success: false, msg: 'Unauthorized.'});
  }
});