I have this data on database , I want that if user search the "ja" the result will display both "james" and "jam"
async getUsername(searchname){
let result = await Database.getConnection().models.User.findAll({
where: {
FirstName: searchname
},
raw: true
});
return result;
},
data
{
Firstname: "james",
....,
},
{
Firstname: "jam",
....,
},
{
Firstname: "Harltan",
....,
},
{
Firstname: "Hames",
....,
}
the current result is either i type or search the ja, no result found, but if i search jam the "jam" data is display
the db i use is mysql
>Solution :
Assuming you’re using Sequelize, you can simply use the startsWith operator
const { Op } = require("sequelize");
// ...
let result = await Database.getConnection().models.User.findAll({
where: {
FirstName: {
[Op.startsWith]: searchname
},
},
raw: true,
});
See the Operators documentation for more information.