【发布时间】:2021-05-20 22:17:35
【问题描述】:
右图是MongoDB代码,左图是Sequelize代码。打印了console.log(findArgs),前端部分没有报错,但是findArgs不起作用。 findArgs 是过滤部分。
Sequelize 代码不起作用。我认为下划线部分是错误的,但我不知道。
它通过按下键值响应后端路由器,但不响应 UI。
MongoDB 代码
router.post('/AllLists', (req, res) => {
let limit = req.body.limit ? parseInt(req.body.limit) : 20;
let skip = req.body.skip ? parseInt(req.body.skip) : 0;
let findArgs = {};
for(let key in req.body.filters) {
if (req.body.filters[key].length > 0) { // continet or price
console.log('key', key)
findArgs[key] = req.body.filters[key];
}
}
console.log(findArgs)
Product.find(findArgs)
.populate("writer")
.skip(skip)
.limit(limit)
.exec((err, productInfo) => {
if(err) return res.status(400).json({ success: false, err })
return res.status(200).json({
success: true, productInfo,
postSize: productInfo.length
})
})
})
序列化代码
router.post('/AllLists', async (req, res, next ) => {
let limit = req.body.limit ? parseInt(req.body.limit) : 20;
let skip = req.body.skip ? parseInt(req.body.skip) : 0;
let findArgs = {};
for(let key in req.body.filters) {
if(req.body.filters[key].length > 0) {
findArgs[key] = req.body.filters[key];
}
}
console.log('findArgs', findArgs);
try {
const productInfo = await Product.findAll({
findArgs,
limit: limit,
offset: skip,
include: [
{
model: Image,
attributes: ['src']
},
],
});
res.status(200).json({ success: true, productInfo, postSize: productInfo.length});
} catch (err) {
console.error(err);
next(err);
}
})
输出到后端路由器。当键值被按下时
findArgs { Brand: [ 2 ] }
Sequelize 中的 findArgs 查询是否错误?
【问题讨论】:
标签: node.js mongodb express sequelize.js