【问题标题】:Node Sequalize Query "where" key interpoloationNode Sequelize 查询“where”键插值
【发布时间】:2016-06-08 16:47:39
【问题描述】:

用户表有一个名为“name”的列和req.body.key = name。 我不想写name = req.body.value。 我如何以某种方式插入 req.body.key? "{req.body.key}" 不起作用,控制台抛出 User.{req.body.key} does not exist

function(req, res, next) {
    User.findAll({
        where: {
            //Question HERE
            req.body.key : req.body.value           
        }
    }).then(...).catch(...)
}

【问题讨论】:

标签: node.js npm sequelize.js


【解决方案1】:

试试这个:

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

ES6(节点> 5-6我不记得了)你也可以:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-05
    • 2022-01-19
    • 1970-01-01
    • 2016-12-19
    • 2014-08-04
    • 1970-01-01
    • 2017-07-16
    相关资源
    最近更新 更多