ujjwalguptaofficial / JsStore

Simplifying IndexedDB with SQL like syntax and promises
http://jsstore.net/
MIT License
858 stars 110 forks source link

Internal error when using count with a where clause with an invalid index (better error message expected) #194

Closed The-MAZZTer closed 3 years ago

The-MAZZTer commented 3 years ago

Observed in version 3.12.0 running in Chrome Version 90.0.4430.11 (Official Build) dev (64-bit)

https://ujjwalguptaofficial.github.io/idbstudio/?db=Demo&query=count(%7B%0A%20%20%20%20from%3A%20%22Customers%22%0A%7D)%0A`

The demo query works:

count({
    from: "Customers"
})

This does not:

count({
    from: "Customers",
    where: {
      Country: "USA"
    }
})

Error Message is: Cannot read property 'columns' of undefined. jsstore.worker.js line 1173 in Base.prototype.getColumnInfo (this object is Count Instance).

Error occurs because the function expects Count Instance to have a tableName property with the current object store name, but it does not.

It looks like the Count Instance constructor fails to populate this field, which other classes for the other query types do.

The constructor starts at line 7285:

    function Instance(query, onSuccess, onError) {
      var _this = _super.call(this) || this;

      _this.onError = onError;
      _this.onSuccess = onSuccess;
      _this.query = query;
      return _this;
    }

I modified it to:

    function Instance(query, onSuccess, onError) {
      var _this = _super.call(this) || this;

      _this.onError = onError;
      _this.onSuccess = onSuccess;
      _this.query = query;
      _this.tableName = query.from;
      return _this;
    }

This appears to resolve the core problem. However a proper fix should include an error message if the caller fails to provide a proper from field and only provides a where, which this fix does not do.

ujjwalguptaofficial commented 3 years ago

actually column Country does not exist & that's why the error. But column country exist.

so this will work -

count({
    from: "Customers",
    where: {
        country: "USA"
    }
})

The count api does not have implementation of column checking but select api have, so select will throw a proper message -

Screenshot from 2021-03-07 12-59-08

I will add column check in count api.

The-MAZZTer commented 3 years ago

It looks like something was wrong with my original query, possibly the index for the column name did not exist (though that should have not been possible) but I can no longer recreate the problem.

Seeing the same error in idbstudio I thought I had reproduced it.

ujjwalguptaofficial commented 3 years ago

@The-MAZZTer fixed in version - 3.12.1 . Please update the package and check.