简体   繁体   中英

Conditionally set a object property

Its possible to remove the .where clause if queryString is null?

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.

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)

As dandavis said you can break your query into two parts

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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