기능정리 - 조건순으로 검색하기

이재근·2022년 8월 18일
0

const findbyaddress = await hosts.findAll({
where: {
[Op.or]: [
{
mainAddress: {

          [Op.substring]: querydata.search,
        },
      },
      { houseInfo: querydata.search },
    ],
  },
  order: [["createdAt", "DESC"]],
  include: [
    {
      model: images,
      required: false,
      attributes: [
        "hostId",
        "postImageURL",
        "thumbnailURL",
        "userImageURL",
      ],
    },
  ],
});
const findbytitle = await hosts.findAll({
  where: {
    title: {
      [Op.substring]: querydata.search,
    },
  },
  order: [["createdAt", "DESC"]],
  include: [
    {
      model: images,
      required: false,
      attributes: [
        "hostId",
        "postImageURL",
        "thumbnailURL",
        "userImageURL",
      ],
    },
  ],
});
let hostContent = await hosts.findAll({
  where: {
    hostContent: {
      [Op.substring]: querydata.search,
    },
  },
  order: [["createdAt", "DESC"]],
  include: [
    {
      model: images,
      required: false,
      attributes: [
        "hostId",
        "postImageURL",
        "thumbnailURL",
        "userImageURL",
      ],
    },
  ],
});
for (let h = 0; h < findbyaddress.length; h++) {
  searchResult.push(findbyaddress[h]);
  searchResultId.push(findbyaddress[h].hostId);
}
for (let p = 0; p < findbytitle.length; p++) {
  if (searchResultId.includes(findbytitle[p].hostId)) {
  } else {
    searchResult.push(findbytitle[p]);
    searchResultId.push(findbytitle[p].hostId);
  }
}
for (let i = 0; i < hostContent.length; i++) {
  if (searchResultId.includes(hostContent[i].hostId)) {
  } else {
    searchResult.push(hostContent[i]);
    searchResultId.push(hostContent[i].hostId);
  }
}
profile
하루 고생하면 코드가 나 대신 일해준다.

0개의 댓글