简体   繁体   English

有条件地设置对象属性

[英]Conditionally set a object property

Its possible to remove the .where clause if queryString is null? 如果queryString为null,可以删除.where子句吗?

I have tried ternary operator in the middle of expression, but doesn't work. 我在表达式中尝试了三元运算符,但不起作用。

Also the equals expression dont allow: null, undefined or empty String. 此外,equals表达式不允许:null,undefined或empty String。

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']}));
                }
            });

Edit : I am using Dynogels (An ORM for Dynamodb) 编辑 :我正在使用Dynogels (一个ORM for Dynamodb)

As dandavis said you can break your query into two parts 正如dandavis所说,你可以将你的查询分为两部分

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']}));
            }
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM