gpt4 book ai didi

express - SequelizeJS 使用带有查询参数的 If 条件

转载 作者:行者123 更新时间:2023-12-03 22:18:12 25 4
gpt4 key购买 nike

我有一个应用程序路由,如果存在查询,我希望能够在我的 where 子句中使用查询参数。我最初的方法是在 get 中使用 if/else 子句,并根据查询参数是否存在返回两个不同的查询,但我的 SyntaxError: Unexpected token . 出现 then(function... 错误,这告诉我这不是正确的方法.如何使用 Sequelize 实现某些目标?

/*====   /  ====*/

appRoutes.route('/')

.get(function(req, res){

console.log(req.query.dataDateStart);
console.log(req.query.dataDateEnd);

if(req.query.dataDateStart && req.query.dataDateEnd){
return models.Comment.findAll({
where: {
dataDateStart: {
$gte: dateFormatting(req.body.dataDateStart)
},
dataDateEnd: {
$lte: dateFormatting(req.body.dataDateEnd)
}
},
order: 'commentDate DESC',
include: [{
model: models.User,
where: { organizationId: req.user.organizationId },
attributes: ['organizationId', 'userId']
}],
limit: 10
})
} else {
return models.Comment.findAll({
order: 'commentDate DESC',
include: [{
model: models.User,
where: { organizationId: req.user.organizationId },
attributes: ['organizationId', 'userId']
}],
limit: 10
})
}

.then(function(comment){
function feedLength(count){
if (count >= 10){
return 2;
} else {
return null;
}
};

res.render('pages/app/activity-feed.hbs',{
comment: comment,
user: req.user,
secondPage: feedLength(comment.length)
});
});
})

.post(function(req, res){
function dateFormatting(date){
var newDate = new Date(date);
return moment.utc(newDate).format();
}

console.log("This is a date test" + dateFormatting(req.body.dataDateStart));
//Testing if the query will come through correctly.
models.Comment.findAll({
order: 'commentDate DESC',
where: {
dataDateStart: {
$gte: dateFormatting(req.body.dataDateStart)
},
dataDateEnd: {
$lte: dateFormatting(req.body.dataDateEnd)
}
},
include: [{
model: models.User,
where: {
organizationId: req.user.organizationId,
},
attributes: ['organizationId', 'userId']
}],
limit: 10
}).then(function(filterValues) {
var dataDateStart = encodeURIComponent(dateFormatting(req.body.dataDateStart));
var dataDateEnd = encodeURIComponent(dateFormatting(req.body.dataDateEnd));
res.redirect('/app?' + dataDateStart + '&' + dataDateEnd);
}).catch(function(error){
res.send(error);
})
});

最佳答案

这是一个语法错误。 then 函数只能在 thenable 对象上调用。在上面截取的代码中, .then 没有应用。相反,它是在 if-else 语句之后调用的。

if(...) {
...
}
else {
...
}
// .then() is not called on any object --> syntax error 'unexpected "."'
.then()

如果您只想配置 where 参数,您可以根据 url 查询定义 where 对象。
appRoutes.route('/')
.get(function(req, res){

console.log(req.query.dataDateStart);
console.log(req.query.dataDateEnd);

var whereObject = {};
// CHeck for queries in url
if(req.query.dataDateStart && req.query.dataDateEnd){
whereObject = {
dataDateStart: {
$gte: dateFormatting(req.body.dataDateStart)
},
dataDateEnd: {
$lte: dateFormatting(req.body.dataDateEnd)
}
};
}

models.Comment.findAll({
where: whereObject,
order: 'commentDate DESC',
include: [{
model: models.User,
where: { organizationId: req.user.organizationId },
attributes: ['organizationId', 'userId']
}],
limit: 10
})
.then(function(comment){
function feedLength(count){
if (count >= 10){
return 2;
} else {
return null;
}
};

res.render('pages/app/activity-feed.hbs',{
comment: comment,
user: req.user,
secondPage: feedLength(comment.length)
});
});
})

.post(function(req, res){
function dateFormatting(date){
var newDate = new Date(date);
return moment.utc(newDate).format();
}

console.log("This is a date test" + dateFormatting(req.body.dataDateStart));
//Testing if the query will come through correctly.
models.Comment.findAll({
order: 'commentDate DESC',
where: {
dataDateStart: {
$gte: dateFormatting(req.body.dataDateStart)
},
dataDateEnd: {
$lte: dateFormatting(req.body.dataDateEnd)
}
},
include: [{
model: models.User,
where: {
organizationId: req.user.organizationId,
},
attributes: ['organizationId', 'userId']
}],
limit: 10
}).then(function(filterValues) {
var dataDateStart = encodeURIComponent(dateFormatting(req.body.dataDateStart));
var dataDateEnd = encodeURIComponent(dateFormatting(req.body.dataDateEnd));
res.redirect('/app?' + dataDateStart + '&' + dataDateEnd);
}).catch(function(error){
res.send(error);
})
});

关于express - SequelizeJS 使用带有查询参数的 If 条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38065643/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com