Javascript 未处理的PromisejectionWarning:TypeError:分配给常量变量
我在findAll()方法之外执行where子句,以允许用户在请求正文中发送或不发送筛选器。我的请求正文中的一个值是categoryId,它可以发送也可以不发送 这是我的代码:Javascript 未处理的PromisejectionWarning:TypeError:分配给常量变量,javascript,node.js,sequelize.js,Javascript,Node.js,Sequelize.js,我在findAll()方法之外执行where子句,以允许用户在请求正文中发送或不发送筛选器。我的请求正文中的一个值是categoryId,它可以发送也可以不发送 这是我的代码: const where = {} if (categoryId) { where = { '$subcategories.category_id$': categoryId } } try { cons
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={}
或
将
where:{where}
更改为where:where
错误实际上非常明显。您将where
声明为常量,然后尝试为其赋值。是的,您的权利。而且我似乎在$subcategories上有一个无效值。category\u id$,我做错了什么吗@这能回答你的问题吗?我在$subcategories上有此错误。category_id$表示无效值,我是否做错了什么?它还在$subcategories中给我一个无效值。category_id$,我是否做错了?请尝试删除$signstrywhere:{…where}
对不起,我不明白,您能重复一下吗?:)或者你可以只做where:where
const where = {}
if (categoryId) {
where['subcategories.category_id'] = categoryId
}