【发布时间】:2020-07-31 09:56:13
【问题描述】:
我在 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()
【问题讨论】:
-
错误其实很明显。您将
where声明为常量,然后尝试为其赋值。 -
是的,你的权利。
-
另外我似乎在 $subcategories.category_id$ 上有一个无效的值,我做错了什么吗? @Aioros
-
我在 $subcategories.category_id$ 上遇到这个错误,说无效值,我做错了什么吗?
标签: javascript node.js sequelize.js