未处理拒绝错误:其中:“原始查询”已被删除 使用[“原始查询”,[替换]]
的地方
我在渲染以下代码时遇到此错误。这是一个动态where
子句,由用户在search
字段中输入的值生成:
var queryWhere = {id: {$ne: null}};
if (req.query) {
if (req.query.gender && req.query.gender !== '') {
searchGender = "gender = '" + req.query.gender + "'";
} else if (req.query.gender && req.query.gender === '') {
searchGender = "gender IS NOT NULL ";
}
if (req.query.experience && req.query.experience !== '') {
searchExperience = "experience = '" + req.query.experience + "'";
} else if (req.query.gender && req.query.experience === '') {
searchExperience = "experience IS NOT NULL ";
}
queryWhere = {
$and: [
{$or: [
searchGender,
searchExperience]}
]
};
models.Users.findAll({
offset: numPerPage * 50,
limit: 50,
where: queryWhere,
include: [
{model: models.Users_Answers}
],
order: [
[models.Sequelize.col('id'), 'ASC'],
[models.Users_Answers, 'id', 'ASC']
]
}).then(function(answers) {
res.render('answers', {answers: answers, search: req.query.searchParam, moment: moment, pagesize: (numPerPage+1), total: totalCount / 50});
});
} else {
res.render('answers');
}
我该如何解决这个问题?
答案 0 :(得分:4)
原始查询应为数组
queryWhere = {
$and: [
{$or: [
[searchGender],
[searchExperience]]}
]
};
答案 1 :(得分:1)
推荐的方法是使用Sequelize自己的运算符
if (req.query.gender && req.query.gender !== '') {
searchGender = {'gender': req.query.gender};
} else if (req.query.gender && req.query.gender === '') {
searchGender = {'gender': {$not: null}};
}
if (req.query.experience && req.query.experience !== '') {
searchExperience = {'experience': req.query.experience};
} else if (req.query.gender && req.query.experience === '') {
searchExperience = {'experience': {$not: null}};
}
答案 2 :(得分:0)
推荐使用Sequelize自己的运算符
var defered = Q.defer();
const offset = queryString.offset * queryString.limit;
const limit = queryString.limit;
var queryWhere = { class_id: { $ne: null }, section_id: { $ne: null } };
var searchClass = {};
var searchSection = {};
if (queryString) {
if (queryString.class && queryString.class !== "") {
searchClass = { class_id: { $eq: queryString.class } };
} else if (queryString.class && queryString.class === "") {
searchClass = { class_id: { $ne: null } };
}
if (queryString.section && queryString.section !== "") {
searchSection = { section_id: { $eq: queryString.section } };
} else if (queryString.section && queryString.section === "") {
searchSection = { section_id: { $ne: null } };
}
}
queryWhere = {
$and: [[searchClass], [searchSection]]
};
const schoolDB = require("../../db/models/tenant")(schema);
const Student = schoolDB.model("Student");
Student.findAll({
attributes: [
"id",
"first_name",
"last_name",
"profile_image_url",
"roll_number",
"emergency_contact_number"
],
offset: offset,
limit: limit,
where: queryWhere,
order: [["roll_number", "ASC"]]
})
.then(result => {
defered.resolve(result);
})
.catch(err => {
defered.reject(err);
});