Javascript 有条件地设置对象属性
如果queryString为null,是否可以删除.where子句Javascript 有条件地设置对象属性,javascript,if-statement,dynogels,Javascript,If Statement,Dynogels,如果queryString为null,是否可以删除.where子句 我在表达式中间尝试了三元算子,但不起作用。 此外,equals表达式不允许:null、未定义或空字符串 CashierHistory .scan() .where('userId').equals(path['userId']) .where('status').equals(queryString['status']) # remove all this
我在表达式中间尝试了三元算子,但不起作用。
此外,equals表达式不允许:null、未定义或空字符串CashierHistory
.scan()
.where('userId').equals(path['userId'])
.where('status').equals(queryString['status']) # remove all this line in case of queryString['status'] is null
.limit(10)
.startKey(queryString)
.exec(function (err, acc) {
if (err == null) {
console.log(acc);
return cb(null, Api.response(acc));
} else {
console.log(err['message']);
return cb(null, Api.errors(200, {5: err['message']}));
}
});
编辑:我正在使用(Dynamodb的ORM)正如丹达维斯所说,您可以将查询分为两部分
var query = CashierHistory
.scan()
.where('userId').equals(path['userId']);
if (queryString['status']) {
query = query.where('status').equals(queryString['status']);
}
return query.limit(10)
.startKey(queryString)
.exec(function (err, acc) {
if (err == null) {
console.log(acc);
return cb(null, Api.response(acc));
} else {
console.log(err['message']);
return cb(null, Api.errors(200, {5: err['message']}));
}
});
您正在使用的库或类似库是什么?在条件之前/之后分成两部分,将左侧指定给变量,添加IF,使用命名的var@T.J.Crowder编辑我的question@dandavis我不喜欢这样做,因为我需要做很多的if/else,因为我将有4个条件,然后将是16 if/else