question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

connection.query is not a function

See original GitHub issue

I have a test suite that queries against a postgres running in a docker container. When I run tests using mock-knex before I run my integration tests, all those tests start failing with the following stack:

20) lib/models/Foo "before all" hook:
     TypeError: insert into "users" ("created_at", "password", "updated_at", "username") values ($1, $2, $3, $4) returning "id" - connection.query is not a function
      at /someProject/node_modules/knex/lib/dialects/postgres/index.js:151:18
      at tryCatcher (/someProject/node_modules/knex/node_modules/bluebird/js/main/util.js:26:23)
      at Promise._resolveFromResolver (/someProject/node_modules/knex/node_modules/bluebird/js/main/promise.js:480:31)
      at new Promise (/someProject/node_modules/knex/node_modules/bluebird/js/main/promise.js:70:37)
      at Client._query (/someProject/node_modules/knex/lib/dialects/postgres/index.js:150:12)
      at Client.query (/someProject/node_modules/knex/lib/client.js:127:24)
      at Object.<anonymous> (/someProject/node_modules/knex/lib/runner.js:116:24)
      at Object.tryCatcher (/someProject/node_modules/knex/node_modules/bluebird/js/main/util.js:26:23)
      at Object.query (/someProject/node_modules/knex/node_modules/bluebird/js/main/method.js:15:34)
      at /someProject/node_modules/knex/lib/runner.js:44:21
      at /someProject/node_modules/knex/node_modules/bluebird/js/main/using.js:176:30
      at tryCatcher (/someProject/node_modules/knex/node_modules/bluebird/js/main/util.js:26:23)
      at Promise._settlePromiseFromHandler (/someProject/node_modules/knex/node_modules/bluebird/js/main/promise.js:507:31)
      at Promise._settlePromiseAt (/someProject/node_modules/knex/node_modules/bluebird/js/main/promise.js:581:18)
      at Promise._settlePromises (/someProject/node_modules/knex/node_modules/bluebird/js/main/promise.js:697:14)
      at Async._drainQueue (/someProject/node_modules/knex/node_modules/bluebird/js/main/async.js:123:16)
      at Async._drainQueues (/someProject/node_modules/knex/node_modules/bluebird/js/main/async.js:133:10)
      at Immediate.Async.drainQueues [as _onImmediate] (/someProject/node_modules/knex/node_modules/bluebird/js/main/async.js:15:14)

I’m using the following hooks in mocha everywhere I run tests using mock-knex:

before(function () {
    db = knex({ client: 'postgres' });
    mockDb.mock(db);
    models = modelsFactory({ knex: db, config });
});
after(function () {
    mockDb.unmock(db);
});
beforeEach(function () {
    tracker.install();
});
afterEach(function () {
    tracker.uninstall();
});

Any idea what I should look for?

The following versions are in play:

  • nodejs v4.2.2
  • bookshelf 0.9.1
  • knex 0.9.0
  • mock-knex 0.3.0

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:13 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
jbrumwellcommented, Jan 28, 2016

It is likely due to unmock not functioning properly? This is a known issue that requires attention.

0reactions
jbrumwellcommented, Sep 15, 2016

No 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

connection.query(...).then is not a function - Stack Overflow
To use .then() with mysql first you need to “promisify” the database client.That can be done by creating a wrapper class for the...
Read more >
[Discord.js] [MySQL] TypeError: connection.query is not a ...
Looking at the example from W3schools, it seems that connection.connect should be called with a callback function in which connection.query can ...
Read more >
TypeError: connection.query(...).on is not a function · Issue #184
Hi, I found this error while generate models: TypeError: connection.query(...).on is not a function at .
Read more >
How to use the mysql.createConnection function in mysql - Snyk
performQuery = function(query, callback) { connection.query(query, function(err, rows, ... + '(`id` int unsigned not null AUTO_INCREMENT PRIMARY KEY, ...
Read more >
Node.js with MySQL - w3resource
createConnection({ host : 'localhost', user : 'root', password : 'datasoft123', }); //Wrong database name connection.query('USE kkk', function( ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found