사용자가 요청 본문에서 필터를 보낼 수 있도록 허용하기 위해 findAll () 메서드 외부에서 where 절을 수행하고 있습니다. 내 요청 본문의 값 중 하나는 전송 가능 여부에 관계없이 categoryId입니다.
이것은 내 코드입니다.
const where = {}
if (categoryId) {
where = { '$subcategories.category_id$': categoryId
}
}
try {
const establishments = await establishment.findAll({
attributes: [
"id",
"description",
"latitude",
"longitude",
[sequelize.literal(' (6371 * acos ( '
+ 'cos( radians(' + latitude + ') ) '
+ '* cos( radians( latitude ) ) '
+ '* cos( radians( longitude ) - radians(' + longitude + ') )'
+ '+ sin( radians(' + latitude + ') )'
+ '* sin( radians( latitude ))))'), 'distance']
],
include: [{
attributes: [],
model: subcategory, as: 'subcategories',
required: false,
},
{
attributes: ["id", "name"],
model: certificate, as: 'certificates',
required: false,
},
],
where: {
where
},
establishments: ['id'],
});
} catch (error) {
console.log(error)
res.status(400).json({ Error: "Error while fetching the establishment" });
}
주어진 오류 :
UnhandledPromiseRejectionWarning: TypeError: Assignment to constant variable.
UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch()
const
일단 선언되면 변수에 값을 할당 할 수 없습니다 .
어느 쪽이든 사용 let
let where = {}
또는
const where = {}
if (categoryId) {
where['subcategories.category_id'] = categoryId
}
변화 where: { where }
에where: where
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다