微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何将[Op.or]附加到NodeJS中的对象以建立Sequelize的条件?

如何解决如何将[Op.or]附加到NodeJS中的对象以建立Sequelize的条件?

在我的NodeJS模型中,我有以下查询

Ad.adList = function(where_condition,lang_code='EN') {
    return this.findAll({
          attributes:['id','name'],where: where_condition,include:[
            { 
                association : 'ad_user',where       : {status:'a',is_ban:'n',deleted_at:null},required    : true
            },{ association: 'translation',where: {lang_code:lang_code},required: false},{ association: 'ad_title',where: {status:'a',required: false}
          ],order: [
            ['translation','name','asc']
          ]
    });
};

在控制器文件中,我建立了where_condition对象,如下所示:-

let where_condition_Obj =   {};
                
if(typeof formData.show_active !== 'undefined' && userIdformData.show_active !== null && formData.show_active !== '')
{
    where_condition_Obj.status = 'a';
}
if(typeof formData.category_id !== 'undefined' && userIdformData.category_id !== null && formData.category_id !== '')
{
    where_condition_Obj.category = formData.category_id;
}

这样,控制器中的where_condition_Obj对象变为:-

where_condition_Obj =   {
                    status      : 'a',category    : 1,}

现在,我需要使用类似操作来运行OR查询。对于这种情况,我的where_condition_Obj应该是这样的

where_condition_Obj =   {
                        status      : 'a',[Op.or]     : [
                                    { 
                                        name: 
                                        { 
                                            [Op.like] : '%' + searchText + '%'
                                        }
                                    },{
                                        description: 
                                        { 
                                            [Op.like] : '%' + searchText + '%'
                                        }
                                    }   
                                ]
                }

但是如何在类似where_condition_Obj.category = formData.category_id;的对象中附加[Op.or]

我尝试过类似的事情

where_condition_Obj.[Op.or] = [
                                    { 
                                        name: 
                                        { 
                                            [Op.like] : '%' + searchText + '%'
                                        }
                                    },{
                                        description: 
                                        { 
                                            [Op.like] : '%' + searchText + '%'
                                        }
                                    }   
                                ]

但这给了我错误。有什么方法可以实现?

注意:我可以从头开始像这样初始化对象,

where_condition_Obj =   {
                        [Op.or]     : [
                                    { 
                                        name: 
                                        { 
                                            [Op.like] : '%' + searchText + '%'
                                        }
                                    },{
                                        description: 
                                        { 
                                            [Op.like] : '%' + searchText + '%'
                                        }
                                    }   
                                ]
                }

但是我不想这样做,因为只会根据可能会或可能不会发送的特定参数附加此条件。

解决方法

您可以使用以下条件数组将where_condition设置为Op.and

[Op.and]: [{
 status: 'a' 
},{
  category: 1,}]

然后在某些情况下,您可以将新条件推入该数组:

where_condition[Op.and].push = {
  [Op.or]: [
     { 
       name: 
        { 
          [Op.like] : '%' + searchText + '%'
        }
     },{
       description: 
        { 
          [Op.like] : '%' + searchText + '%'
        }
      }   
    ]
}
,

今天,我面临着这种情况,经过几分钟的研究,我对如何使该功能正常工作有了一些基本认识。

下面是对我有用的方法。

OR条件声明另一个对象。

let where_condition_Obj = {};
let or_condition_Obj = {};

对于正常的where条件,请按照您的方式进行操作。 例如:

if (typeof formData.show_active !== 'undefined' && userIdformData.show_active !== null && formData.show_active !== '') {
      where_condition_Obj.status = 'a';
}

if (typeof formData.category_id !== 'undefined' && userIdformData.category_id !== null && formData.category_id !== '') {
      where_condition_Obj.category = 1;
}

对于OR条件,请遵循以下方法

if (typeof formData.name !== 'undefined' && userIdformData.name !== null && formData.name !== '') {
     or_condition_Obj.name = { [Op.substring]: 'name' }
}

if (typeof formData.description !== 'undefined' && userIdformData.description !== null && formData.description !== '') {
     or_condition_Obj.description = { [Op.substring]: 'description' }
}

最后,在您的where条件下,同时获得这两个对象并按如下所示进行分配。

where: {
    [Op.or]: or_condition_Obj,[Op.and]: where_condition_Obj
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。