Search code examples
postgresqlsails.jswaterlinesails-postgresql

sails.js unable to lift sails version 0.9.9 and sails-postgresql version 0.9.7


I am not able to lift sails version 0.9.9 with postgresql version 0.9.7

Here is the error message that I get

debug: Lowering sails...

/home/mandeep/iqr/thirstt/node_modules/sails-postgresql/lib/query.js:285
      if(self._schema[key].type === 'text') caseSensitive = false;
                          ^
TypeError: Cannot read property 'type' of undefined
  at Object.sql.and (/home/mandeep/iqr/thirstt/node_modules/sails-postgresql/lib/query.js:285:27)
  at /home/mandeep/iqr/thirstt/node_modules/sails-postgresql/lib/query.js:256:19
  at Array.forEach (native)
  at [object Object].Query.where (/home/mandeep/iqr/thirstt/node_modules/sails-postgresql/lib/query.js:195:24)
  at /home/mandeep/iqr/thirstt/node_modules/sails-postgresql/lib/query.js:119:14
  at Array.forEach (native)
  at [object Object].Query._build (/home/mandeep/iqr/thirstt/node_modules/sails-postgresql/lib/query.js:115:32)
  at [object Object].Query.find (/home/mandeep/iqr/thirstt/node_modules/sails-postgresql/lib/query.js:35:21)
  at __FIND__ (/home/mandeep/iqr/thirstt/node_modules/sails-postgresql/lib/adapter.js:362:40)
  at after (/home/mandeep/iqr/thirstt/node_modules/sails-postgresql/lib/adapter.js:506:7)
  at /home/mandeep/iqr/thirstt/node_modules/sails-postgresql/lib/adapter.js:492:7

It was running fine earlier. Not sure what went wrong. I have tried removing node_modules directory and doing a npm install after npm cache clear but still keep getting this error. I cannot upgrade to sails version 0.10 since its quite a big project and upgrade will be a really big effort. How can I fix this issue in the same version ? Any help will be appreciated.


Solution

  • I figured out the issue. Seems like a sails-postgresql issue to me. I was doing a findOne on a model in config/bootstrap.js and it was failing when there was no record in that table

    Here is the part where it fails ( node_modules/sails-postgresql/lib/query.js ):

    Query.prototype.operators = function() {
    
      var self = this;
    
      var sql = {
        and: function(key, options, comparator) {
          var caseSensitive = true;
    
          // Check if key is a string
          if(self._schema[key].type === 'text') caseSensitive = false;
    
          processCriteria.call(self, key, options, '=', caseSensitive);
          self._query += (comparator || ' AND ');
        },
    

    I logged the value of self. When data exists in the table that I queried in bootstrap.js, self looks like this

    { _values: 
       [ 2,
         1,
         'Mon, 29 Sep 2014 09:46:08 GMT',
         'Thu, 06 Nov 2014 08:24:34 GMT' ],
      _paramCount: 5,
      _query: 'UPDATE "cache" SET "val" = $1, "id" = $2, "createdAt" = $3, "updatedAt" = $4 ',
      _schema: 
       { val: { type: 'integer' },
         id: { type: 'integer', primaryKey: true, autoIncrement: true },
         createdAt: { type: 'timestamp with time zone' },
         updatedAt: { type: 'timestamp with time zone' } } 
    }
    

    However, when there is no data in the table, then self looks like this

    { _values: [],
      _paramCount: 1,
      _query: 'SELECT * FROM "cache" ',
      _schema: true 
    }
    

    And then it accesses self._schema[key].type which raises the error since self._schema is not an object in this case. On inserting data to the table, things got fixed but I believe it's a logical bug in sails-postgresql since it should work fine even if there is no data in the table.