Wednesday, 31 May 2017

Undefined binding(s) error in bookshelfJs to restrict returned columns

SERVICE

var Model = require('./../models');

var myService = function (query_params, callback) {

    Model.User
        .forge()
        .fetch({
                withRelated: [{
                    'org': function (qb) {
                        qb.column('id', 'name');
                    },
                    'role': function (qb) {
                        qb.column('id', 'role');
                    }
                }]
            })
        .then(function (collection) {
            return callback(null, collection);
        })
        .catch(function (err) {
            return callback(err, null);
        });
};

MODEL

    var User = Bookshelf.Model.extend({
       tableName: 'user',
       org : function () {
         return this.belongsTo(Org, 'org_id');
      },
      role : function () {
         return this.belongsTo(Role, 'role_id');
      }
    });
    var Users =  Bookshelf.Collection.extend({
        model: User
    });


    var Org = Bookshelf.Model.extend({
        tableName: 'org',
        user : function () {
            return this.hasMany(User);
        }
    });
    var Orgs = Bookshelf.Collection.extend({
        model: Org
    });

    var Role = Bookshelf.Model.extend({
        tableName: 'roles',
        user: function() {
            return this.hasMany(User);
        }
    });


    module.exports = {
        User: User,
        Users: Users,
        Org: Org,
        Orgs: Orgs
    };

The output I am getting the all fields of the user data, and specified field of respective user and role table,

How can I restrict the fields for user table.

I tried this:

   .fetch({
          withRelated: [{
           'org': function (qb) {
               qb.column('id', 'name');
           },
           'role': function (qb) {
               qb.column('id', 'role');
           }
          }],
     columns : ['id']
     })

However, I am getting error :

Error: Undefined binding(s) detected when compiling SELECT query: 



via atjoshi

No comments:

Post a Comment