0

User table has a column called "name" and req.body.key = name. I don't want to write name = req.body.value. How do i somehow interpolate req.body.key? "{req.body.key}" doesn't work and console throws User.{req.body.key} does not exist.

function(req, res, next) { User.findAll({ where: { //Question HERE req.body.key : req.body.value } }).then(...).catch(...) } 
2
  • this solves it: stackoverflow.com/questions/5640988/… Commented Jun 8, 2016 at 17:12
  • What about the answer below? It has the advantage of showing ES6 too. Commented Jul 5, 2016 at 14:02

1 Answer 1

1

Try this:

function(req, res, next) { var whereClause = {}; whereClause[req.body.key] = req.body.value; extend(whereClause, req.body); User.findAll({ where: whereClause }).then(...).catch(...) } 

In ES6 (node > 5-6 I don't remember) you can also:

function(req, res, next) { extend(whereClause, req.body); User.findAll({ [req.body.key]: eq.body.value }).then(...).catch(...) } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.