Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.pluck function in strongloop loopback?

Does storongloop loopback has pluck function for a given model ?

For a Product model,

in Rails I can write

Product.where(some_condition).pluck(:name)

It will return an array of names of products matching the condition.

Is there anything similar in loopback?

PS: I know I can use fields filter and then use underscore( or lodash)'s pluck but that's a two step process.

like image 511
geek_guy Avatar asked Dec 12 '25 12:12

geek_guy


1 Answers

Select specific columns:

{"fields":{"name":true, "email":true}}

Where condition:

{"where":{"id":2}} 

Combining:

{"fields":{"id":true},"where":{"id":{"inq":[10,20,30]}}}

The above code works in swagger. Node.js code would be as follows:

var m = server.models.customer;
    m.findOne({
        fields: ['name', 'email'],
        where: {
            id:{inq:[10,20,30]}}
        }
    }, function (err, data) {
        console.log(data);
    })
like image 88
Anoop Thiruonam Avatar answered Dec 14 '25 10:12

Anoop Thiruonam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!