Cris69
Cris69

Reputation: 590

Query from Angular.js to Sails.js and waterline using populate on a nested model

In my Sails.js I have a model called Invoices.js with the following structure:

 module.exports = {

    tableName:'invoices',
    adapter: 'mysql',
    autoPK:false,
    autoCreatedAt: true,
    autoUpdatedAt: true,

attributes: {

    id: {type: 'integer', autoIncrement: true, primaryKey: true},
    doc_mod: {type:'string'},
    doc_n: {type:'integer'},
    createdAt: {type:'date'},
    updatedAt: {type:'date'},
    settled: {type:'integer'},
    invoice_line: {collection:'invoice_lines', via:'invoice'},
    customer: {model:'customers'},
     booking:{collection:'bookings',via:'invoice'}
  }
}

I would like to query from Angular.js finding customers by 'name' or 'surname'and on client side I came up with this:

.factory('PaymentsService',['$resource',function($resource){

        return{

            Query:function(cursor,sorting,name,surname){
                var allPayments = $resource('http://localhost:1337/invoices');
                return allPayments.query({populate:{'customers':{where:{or:[{name:{contains:name}},{surname:{contains:surname}}]}}},skip:cursor,limit:100,sort:sorting}).$promise;
            }
}

It doesn't seems to work properly I get always this result which is not complete and correct :

.....{
"customer": 1,
"id": 4,
"doc_mod": "receipt",
"doc_n": 3,
"createdAt": "2015-05-11T17:07:40.000Z",
"updatedAt": "2015-05-11T17:07:40.000Z",
"settled": 0
},
{
"customer": 1,
"id": 5,
"doc_mod": "receipt",
"doc_n": 4,
"createdAt": "2015-05-11T18:26:55.000Z",
"updatedAt": "2015-05-11T18:26:55.000Z",
"settled": 0
}.....

Upvotes: 4

Views: 564

Answers (2)

Sergiy Voytovych
Sergiy Voytovych

Reputation: 497

if want two or more populate

var allPayments = $resource('http://localhost:1337/customers');

allPayments.query({
    where:{or:[{name:{contains:name}}, {surname:contains:surname}}]},
    populate:['invoices','some_other_populate'],
    skip:cursor,
    limit:100,
    sort:sorting
}).$promise;

Upvotes: 0

Meeker
Meeker

Reputation: 5979

You currently can't query sub-documents in this fashion. You need to reverse your query so that you filter customers and then return invoices associated. It may not be 100% what your looking for, but waterline is working on this issue currently!

var allPayments = $resource('http://localhost:1337/customers');

allPayments.query({
  where:{or:[{name:{contains:name}}, {surname:contains:surname}}]},
  populate:'invoices',
  skip:cursor,
  limit:100,
  sort:sorting
})
.$promise

Upvotes: 1

Related Questions