node-orm2 icon indicating copy to clipboard operation
node-orm2 copied to clipboard

.only(...) not working in chaining mode

Open hoseinmobasher opened this issue 12 years ago • 3 comments

Hello,

I am working on node-orm2 in postgres. So, I want to define options on my read operation. I am using from chaining operations and all things are okay except only() operation. The following code is what I try:

var chain = model.find({ *some conditions* });

if (typeof this.limit !== 'undefined')
    chain = chain.limit(this.limit);

if (typeof this.offset !== 'undefined')
    chain = chain.offset(this.offset);

return chain.only("col1", "col2").run(callback);

But the result includes all of columns not only col1 and col2. Can you explain me what was wrong ?

EDIT 1 I trace the whole my code and node-orm2 module. I think the problem is relate to /orm -> lib -> ChainFind.js -> all function.

    all: function (cb) {
        opts.driver.find(opts.only, opts.table, opts.conditions, {
            limit  : opts.limit,
            order  : opts.order,
            merge  : opts.merge,
            offset : opts.offset,
            exists : opts.exists
        }, function (err, data) {
            if (err) {
                return cb(err);
            }
            if (data.length === 0) {
                return cb(null, []);
            }
            var pending = data.length;

            for (var i = 0; i < data.length; i++) {
                (function (idx) {
                    opts.newInstance(data[idx], function (err, instance) {
                        data[idx] = instance;

                        if (--pending === 0) {
                            return cb(null, data);
                        }
                    });
                })(i);
            }
        });
        return this;
    }

I log the results before running the for loop, It was what I want! But after running the loop, It selects all the fields. But all the fields is null except the columns what I selected! Result is same as below :

[
  {
    "username": "hossein",
    "password": null,
    "dispname": "Hossein Mobasher",
    "email": null,
    "contact_url": null,
    "privacy_url": null,
    "status": null,
    "telephone": null,
    "average_rate": null,
    "birth_date": null,
    "locale": null,
    "age": null,
    "registered_date": null,
    "verified": null,
    "verification_code": null,
    "verified_date": null,
    "cash": null
  }
]

Cheers, Hossein

hoseinmobasher avatar Aug 03 '13 15:08 hoseinmobasher

+1 Experiencing the same problem

jamhall avatar Jul 20 '14 20:07 jamhall

I have a similar issue. I'm running orm 2.1.19 with sqlite3 (2.1.7).

When I execute model.find({ conditions }).only('id').all(function(result){ ... });

It returns:

[
  {
    "id" : 1,
    "name" : null,
    "status" : null
  },
  {
    "id" : 2,
    "name" : null,
    "status" : null
  }
]

Has a solution/workaround been found since this was first posted?

mickjones701 avatar Oct 26 '14 21:10 mickjones701

I have also same problem any solution for this?

akshaykumar12527 avatar Sep 27 '16 22:09 akshaykumar12527