Reputation: 163
I need your help. I am working on a web server with node.js and reactJS. I want to make a page for the Admin called "Users" to search for the users in the SQL database. I wrote a code that create two options to search Users by "Name" or "Country" this is the code:
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},
In the query file I wrote this code:
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, })
Now these two pieces of codes create a text-box "Name" and a drop-down menu "Country" and the Admin have to select the exact username and the correct country to get a result.
I need a sequelize query that let the Admin to enter the name (either first or last name) OR the country and get the result with any matching name or partially match or with the matching country.
Additional: I don't know if it possible, I want the to results to appear during that the admin is writing the name ( matching every typed letter and show the results ) . Thanks in advance for anyone can help.
Upvotes: 1
Views: 1918
Reputation: 163
Solved: I Edited my if statement to be like this :
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 , })
(where) is an object that contains the clause (or), it do Select from database firstname or lastname containing (args.firstname) -that had been entered by the Admin-
Ex: I want to search for the user "John" in the database, if I typed "Jo" it will get the result with all users that have "John" as their firstname or lastname from database as they containing "Jo" in their name.
Upvotes: 2