Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何在Knex.js中动态创建where条件?_Node.js_Knex.js - Fatal编程技术网

Node.js 如何在Knex.js中动态创建where条件?

Node.js 如何在Knex.js中动态创建where条件?,node.js,knex.js,Node.js,Knex.js,我需要根据收到的参数使用where子句创建一个查询,但我不知道如何在where子句中包含一些参数。在下面的代码中,四个第一个条件(customerId、companyId、invoiceId和open)运行良好,但我不知道如何包括其他条件(closed、openedFrom和openedTo) const getByParams=(请求、恢复)=>{ 常量条件={} if(req.query.customerId)条件['wo.customerId']=req.query.customerId

我需要根据收到的参数使用where子句创建一个查询,但我不知道如何在where子句中包含一些参数。在下面的代码中,四个第一个条件(customerId、companyId、invoiceId和open)运行良好,但我不知道如何包括其他条件(closed、openedFrom和openedTo)

const getByParams=(请求、恢复)=>{
常量条件={}
if(req.query.customerId)条件['wo.customerId']=req.query.customerId
if(req.query.companyId)条件['wo.companyId']=req.query.companyId
if(req.query.invoiceId)条件['wo.invoiceId']=req.query.invoiceId
如果(请求查询打开)条件['wo.finishedAt']=null
const onlyClosed=req.query.closed?'wo.finishedAt!=null':“”
const openedFrom=req.query.openedFrom?`wo.openingAt>=${req.query.openedFrom}`:“”
const openedTo=req.query.openedTo?`wo.openingAt{
const json=json.stringify(wo)
const newJson=convertJson(json)
res.status(200).send(newJson)
})
.catch(err=>res.status(500).send(err))
}
请参阅

试试这个:

.where((builder) => {

    if (req.query.customerId)
        builder.where('wo.customerId', req.query.customerId);

    if (req.query.companyId)
        builder.where('wo.companyId', req.query.companyId);

    if (req.query.invoiceId)
        builder.where('wo.invoiceId', req.query.invoiceId);

    if (req.query.open)
        builder.whereNull('wo.finishedAt');

    // so on..

});
而不是

.where(conditions)

谢谢@vedsmith92。这正是我想要的,它解决了我的问题。非常感谢你
.where(conditions)