Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getDatastore().sendNativeQuery returns error is not a function #4318

Closed
tbowler opened this issue Feb 15, 2018 · 8 comments
Closed

getDatastore().sendNativeQuery returns error is not a function #4318

tbowler opened this issue Feb 15, 2018 · 8 comments

Comments

@tbowler
Copy link

tbowler commented Feb 15, 2018

Sails version: v1.0.0-46
Node version: v8.9.1
NPM version: v5.6.0
DB adapter name: sails-postgres
DB adapter version: 1.0.0-12
Operating system: macOS Sierra v10.12.6


The issue happens when trying to do a native query on a model. Activation is the name of my model

var qry = 'SELECT * FROM activation WHERE "expiresOn"::timestamp > now()';
var valuesToEscape = [];
Activation.getDatastore().sendNativeQuery(qry, valuesToEscape, function(err, rawResult) {
  console.log(rawResult);
  console.log(err);
});

The model is simple

const stockCreate = require('waterline/lib/waterline/methods/create');
const moment = require('moment');

module.exports = {
  primary_key: 'id',

  attributes: {
    id: {
      type: 'string',
      required: true,
      unique: true,
    },
    email: {
      type: 'string',
    },
    firstName: {
      type: 'string',
    },
    lastName: {
      type: 'string',
    },
    expiresOn: {
      type: 'string',
      defaultsTo: `${moment().add(30, 'days').format('YYYY-MM-DD HH:mm:ss')}`,
    },
  },
  create: function(newRecord, explicitCbMaybe, metaContainer) {
    return stockCreate.bind(this)(newRecord, explicitCbMaybe, metaContainer);
  },
};

The error returned is always

TypeError: options.driver.sendNativeQuery(...).switch is not a function
  at Object.during (/newSailsApp/node_modules/sails-hook-orm/lib/datastore-method-utils/help-send-native-query.js:61:16)
  at _makeCallToDuringFn (/newSailsApp/node_modules/sails-hook-orm/lib/datastore-method-utils/private/do-with-connection.js:84:36)
  at /newSailsApp/node_modules/sails-hook-orm/lib/datastore-method-utils/private/do-with-connection.js:108:7
  at /newSailsApp/node_modules/sails-hook-orm/lib/datastore-method-utils/private/do-with-connection.js:66:14
  at Object.liveMachine._configuredExits.success (/newSailsApp/node_modules/machinepack-postgresql/node_modules/machine/lib/private/help-configure-machine-instance.js:110:16)
  at afterMaybeArtificiallyWaiting (/newSailsApp/node_modules/machinepack-postgresql/node_modules/machine/lib/private/intercept-exit-callbacks.js:406:21)
  at maybeArtificiallyWait (/newSailsApp/node_modules/machinepack-postgresql/node_modules/machine/lib/private/intercept-exit-callbacks.js:220:20)
  at afterPotentiallyCaching (/newSailsApp/node_modules/machinepack-postgresql/node_modules/machine/lib/private/intercept-exit-callbacks.js:240:11)
  at _cacheIfAppropriate (/newSailsApp/node_modules/machinepack-postgresql/node_modules/machine/lib/private/intercept-exit-callbacks.js:98:18)
  at Function._interceptExit [as success] (/newSailsApp/node_modules/machinepack-postgresql/node_modules/machine/lib/private/intercept-exit-callbacks.js:111:9)
  at cb (/newSailsApp/node_modules/machinepack-postgresql/machines/get-connection.js:87:20)
  at BoundPool.<anonymous> (/newSailsApp/node_modules/pg-pool/index.js:137:9)
  at /newSailsApp/node_modules/generic-pool/lib/generic-pool.js:347:9
  at BoundPool.<anonymous> (/newSailsApp/node_modules/pg-pool/index.js:103:7)
  at Connection.<anonymous> (/newSailsApp/node_modules/pg/lib/client.js:158:7)
  at Object.onceWrapper (events.js:315:30)
  at emitOne (events.js:121:20)
  at Connection.emit (events.js:211:7)
  at Socket.<anonymous> (/newSailsApp/node_modules/pg/lib/connection.js:121:12)
  at emitOne (events.js:116:13)
  at Socket.emit (events.js:211:7)
  at addChunk (_stream_readable.js:263:12)
  at readableAddChunk (_stream_readable.js:250:11)
  at Socket.Readable.push (_stream_readable.js:208:10)
  at TCP.onread (net.js:594:20)
@sailsbot
Copy link

@tbowler Thanks for posting, we'll take a look as soon as possible.


For help with questions about Sails, click here. If you’re interested in hiring @sailsbot and her minions in Austin, click here.

@tbowler
Copy link
Author

tbowler commented Feb 15, 2018

Please note that I referenced issue #4310 but was unable to find any pertinent help. I tried the possible fix offered by @sgress454 but the issue remains unchanged.

@tbowler
Copy link
Author

tbowler commented Feb 15, 2018

Further Reasearch

So I tried stepping down through all the revisions of sails-postgresql from 1.0.0-12 to 1.0.0-5. Under closer inspection of the stack trace I found that the culprit was sails-hook-orm. I checked and initially I was running 2.0.0-22.

Upgrade sails-hook-orn (Failed)

I tried moving forward to 2.0.0-23 but that still did not work.

The Solution

I had to downgrade sails-hook-orm to 2.0.0-21, and that fixed the issue. Digging a little deeper it appears that the code was changed on January 30, 2018 to fix a sails issue #4271.
This is the line in question; L61

The commit that cause the problem 1db6353dcd78c1abb18b3ab99edb538125927c51

@sgress454
Copy link
Member

@tbowler It sounds like you may have some mismatched dependency versions. Would you mind doing a full reset of your modules and trying again with the latest released sails-hook-orm?

rm package-lock.json
rm -rf node_modules && npm install

@sgress454
Copy link
Member

sgress454 commented Feb 16, 2018

Actually @tbowler is there any pertinent difference between this issue and #4310 that you referenced above? I'm inclined to close this as a duplicate and you can jump in on that one as needed...

@tbowler
Copy link
Author

tbowler commented Feb 16, 2018

@sgress454 I tried the full reset with no change. I am sorry I referenced the wrong issue instead of #4271 it should have been #4310. The reason I opened a second issue was because that issue looked like a dead end, but if you want to reference my findings in that issue then by all means. I am for whatever makes sense and results in a working environment 😄

@sgress454
Copy link
Member

No problem -- I did confirm the issue, and have some fixes in place, but want to get @mikermcneil's eyeballs on them before merging since it touches more of his code. I'll keep you posted on #4310.

@Cazaimi
Copy link

Cazaimi commented Sep 26, 2019

Had the same problem with the sails-mysql adapter. @tbowler's solution works. Downgrade sails-hook-orm to 2.0.0-21.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

4 participants