Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

No strict search using nodejs

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading