Where criteria is not working on populate in waterline

261 views Asked by At

I am using waterline and waterlinhe-orientdb. I have user and order vertex classes and bought edge class User ---Bought-->Orders I am trying to apply Where criteria on populate. But is not working on populate. Here is my code

var CREDIT_CARD = 1; 
User.find({id:userid}).populate('boughts',{where:{payment_method:CREDIT_CARD}})

Schema

USER = {
    identity: 'user',
    connection: 'myLocalOrient',
    attributes:{
    status: { type:"integer", columnName:"status"},
    fname: { type:'string'},
    lname: { type:'string'},
    boughts: {collection: 'orders',through: 'boughts',via: 'user',dominant: true}
};

Bought = {
    identity:'boughts',
    connection: 'myLocalOrient',
    attributes:{
        status:{type:'integer', columnName:'status'},
        buyers:{
                  columnName: 'out',
                  type: 'string',
                  foreignKey: true,
                  references: 'users',
                  on: 'id',
                  onKey: 'id',
                  via: 'orders'
        },
        orders:{
                  columnName: 'in',
                  type: 'string',
                  foreignKey: true,
                  references: 'orders',
                  on: 'id',
                  onKey: 'id',
                  via: 'buyer'
        }
      }
};

Order = {
    identity:'orders',
    connection: 'myLocalOrient',
    attributes:{
            status:{type:'integer',columnName:'status'},
            payment_method:{type:'integer', columnName:'payment_method'},
            boughts:{collection:'users', through:'boughts',via:'orders'}
    }

};
1

There are 1 answers

1
Dário On

9me, from your model definition I can see you are using many-to-many through associations which are not yet officially supported by Waterline. This means that some functionality may not be fully operational and that some errors may occur. For more details read When Many-to-Many Through Associations release? #705.

Your query is also not correct:

var CREDIT_CARD = 1; 
User.find({id:userid}).populate('boughts',{where:{payment_method:CREDIT_CARD}})

The criteria in .populate() does not take the key where.

Here's an example from the documentation:

// Collection Filtering
User.find()
.populate('foo', { type: 'bar', limit: 20 })
.exec(function(err, users) {});

So, in your case you should use:

var CREDIT_CARD = 1; 
User.find({ id: userid }).populate('boughts', { payment_method: CREDIT_CARD })