续集查询选择匹配或部分匹配名称
问题描述:
我需要你的帮助。我正在使用node.js和reactJS在Web服务器上工作。我想为Admin创建一个名为“Users”的页面来搜索SQL数据库中的用户。我写了创建两个选项来搜索由“名”或“国家” 用户这是代码代码:续集查询选择匹配或部分匹配名称
UserSearch: {
fields : {
firstname: {value:'', type:'text', label:"Name", placeholder:'type name', bluredBefore:false, validators:['non'], errorReport:[]},
country: {value:'-1', type:'select', label:"Country", placeholder:'select country', staticDataSource:'countries', bluredBefore:false, validators:['non'], errorReport:[]},
},
errorReport:false,
isWaiting : false,
queryTarget: 'userFind',
queryView: 'UserSearchGrid',
queryViewFields: 'userId, firstname, lastname ',
formCols:3,
notification: '',
},
UserSearchGrid: {searchForm:'UserSearch',
viewFields: ['userId','firstname','lastname'],
dataSource:[], count:0, offset:0, limit:10},
在查询文件我写了这个代码:
var where = {userId:userId,};
if (typeof args.firstname !== 'undefined' && args.firstname !== '')
where['firstname'] = args.firstname;
if (typeof args.country !== 'undefined' && args.country !== '-1')
where['country'] = args.country;
items = await UserProfile.findAndCountAll({
where: where, })
现在这些两段代码创建一个文本框“名称”和一个下拉菜单“国家”,管理员必须选择确切的用户名和正确的国家才能得到结果。
我需要一个允许管理员输入名称(姓或名)或国家并使用任何匹配名称或部分匹配或与匹配的国家/地区获得结果的后续查询。
附加信息:我不知道是否有可能,我希望在管理员写入名称(匹配每个键入的字母并显示结果)期间显示结果。预先感谢任何人都可以提供帮助。
答
解决: 我编辑我的if语句是这样的:
var where = {};
if (typeof args.firstname !== 'undefined' && args.firstname !== '')
// where['firstname'] = args.firstname;
where['$or']={firstname:{$like: `%${args.firstname}%`},lastname:{$like: `%${args.firstname}%`}};
items = await UserProfile.findAndCountAll({
where: where , })
(这里)是包含条款(或者),其执行从含(参数数据库名字或姓氏选择的对象。例如,我想在数据库中搜索用户“John”,如果我输入“Jo”,它将得到所有具有“John”的用户的结果,作为数据库的名字或姓氏,因为他们的名字中包含“Jo”。