javascript - 如何查找用户的名字字母

标签 javascript node.js postgresql sequelize.js

我怎样才能找到名字中带有第一个字母的用户,例如我的名字是“Nathan”,当我在搜索输入中输入“n”时,它会显示用户以“n”开头,而不是不以字母“n”开头的用户"但包含字母 "n",如亨利、康纳..
这是我的 searchController.js:

exports.searchAll = async (req, res) => {
  // Grab query
  const query = req.params.q;

  // Search for user's
  const usersFound = await models.User.findAll({
    where: {
      [Op.or]: [
        {
          fullname: {
            [Op.iLike]: "%" + query + "%",
          },
          // Only include full account users
          passwordhash: {
            [Op.ne]: null, // full account users have a password
          },
          verifiedDT: { [Op.ne]: null },
        },
        {
          institution: {
            [Op.iLike]: "%" + query + "%",
          },
          // Only include full account users
          passwordhash: {
            [Op.ne]: null, // full account users have a password
          },
          verifiedDT: { [Op.ne]: null },
        },
      ],
    },
    attributes: [
      "fullname",
      "public_user_id",
      "institution",
      "location",
      "webpage",
      "linkedin",
      "major",
      "bio",
      "picture",
      "id",
    ],
    include: [
      {
        model: models.Rating,
        attributes: ["skillset_rating", "team_member_rating"],
      },
      {
        model: models.Skill,
        attributes: ["skill"],
      },
    ],
  });

  // Search for teams
  const teamsFound = await models.Team.findAll({
    where: {
      title: {
        [Op.iLike]: "%" + query + "%",
      },
      status: {
        [Op.ne]: "Closed", // past teams
      },
      creatorId: {
        [Op.ne]: null,
      },
    },
    attributes: ["public_team_id", "title", "mission", "status"],
    include: [
      {
        // Creators name
        model: models.User,
        attributes: ["fullname"],
      },
    ],
  });

  // Run searches
  const searchData = await Promise.all([usersFound, teamsFound]);
  res.status(200).json(searchData);
};
这是我的模型 user.js:
module.exports = (sequelize, DataTypes) => {
  const User = sequelize.define(
    "User",
    {
      id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
        allowNull: false,
      },
      fullname: {
        type: DataTypes.STRING,
        allowNull: false,
      },
      passwordhash: DataTypes.STRING,
      institution: DataTypes.STRING,
      bio: DataTypes.STRING,
      creator_user_id: DataTypes.UUID,
      public_user_id: DataTypes.STRING,
      picture: DataTypes.STRING(300),
      email: { type: DataTypes.STRING, unique: true },
      gender: DataTypes.STRING,
    },
    {
      tableName: "Users",
      timestamps: true,
      indexes: [
        {
          unique: false,
          fields: ["email", "id", "fullname", "public_user_id"],
        },
      ],
    }
  );

最佳答案

您的查询当前正在寻找 fullname ILIKE '%n%' ,这意味着字母 n 之前或之后的任意字符组合。如果只想获得以字母 n 开头的结果,请删除第一个 % 通配符。

关于javascript - 如何查找用户的名字字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67837981/

相关文章:

javascript - jquery animate 隐藏滚动条

javascript - Mongoose - 无法调用方法 'findOne'

node.js - NodeJS 流出 AWS Lambda 函数

javascript - Node JS 异步/等待 Controller 问题

javascript - Node JS : Array loop and function Not understanding how callbacks and promises work

database - Postgres 转储更改查询

JavaScript : promise issue

javascript - 解释复杂的 JavaScript for 循环

sql - 在 PostgreSQL 中使用子查询确定行之间的关系

c++ - 使用 C++ 与 libpq 链接错误