Having some trouble with integrating Bookshelf and Knex into my stack. When attempting to perform db read/write, I get a TypeError: knex is not a function.
My bookshelf.js:
'use strict'
var knex = require('knex')(require('./knexfile')).debug(true);
var bookshelf = require('bookshelf')(knex);
bookshelf.plugin('registry');
module.exports = bookshelf;
My god.model.js:
var bookshelf = require('./bookshelf');
var God = bookshelf.Model.extend({
tableName: 'gods'
});
module.exports = bookshelf.model('God', God);
Lastly, the function where data is being fetched:
var validate_key_secret = function(key, secret, callback) {
God.where({apikey: key}).fetch().then(function(result) {
if(result.attributes.apisecret === secret) {
callback(results);
} else {
callback(false);
}
});
}
Calling the function throws the following error:
TypeError: bookshelf.knex is not a function at builderFn [as _builder] (D:\Repositories\knextest\node_modules\bookshelf\lib\bookshelf.js:314:27) at Object.query (D:\Repositories\knextest\node_modules\bookshelf\lib\helpers.js:44:23) at query (D:\Repositories\knextest\node_modules\bookshelf\lib\model.js:1243:30) at where (D:\Repositories\knextest\node_modules\bookshelf\lib\model.js:1278:23) at Function.Model.(anonymous function).Collection.(anonymous function) [as where] (D:\Repositories\knextest\node_modules\bookshelf\lib\bookshelf.js:333:28) at validate_key_secret (D:\Repositories\knextest\app\controllers\gods.server.controller.js:114:3) at Layer.handle [as handle_request] (D:\Repositories\knextest\node_modules\express\lib\router\layer.js:95:5) at next (D:\Repositories\knextest\node_modules\express\lib\router\route.js:131:13) at Route.dispatch (D:\Repositories\knextest\node_modules\express\lib\router\route.js:112:3) at Layer.handle [as handle_request] (D:\Repositories\knextest\node_modules\express\lib\router\layer.js:95:5) at D:\Repositories\knextest\node_modules\express\lib\router\index.js:277:22 at Function.process_params (D:\Repositories\knextest\node_modules\express\lib\router\index.js:330:12) at next (D:\Repositories\knextest\node_modules\express\lib\router\index.js:271:10) at SessionStrategy.strategy.pass (D:\Repositories\knextest\node_modules\passport\lib\middleware\authenticate.js:325:9) at SessionStrategy.authenticate (D:\Repositories\knextest\node_modules\passport\lib\strategies\session.js:71:10)
What I've tried so far:
1) Investigating the bookshelf.js node module. The error is occurring in ./node_modules/bookshelf/lib/bookshelf.js line 314:
builder = bookshelf.knex(tableNameOrBuilder);
When I change the line to,
builder = bookshelf.knex.select().from(tableNameOrBuilder);
I am able to perform simple sql reads and writes. However, this starts to cause issues with more complex queries and I figure that it isn't generally good practice to edit node modules. However, doing this does rule out any possible problems with my knexfile/db configuration.
2) Integrating bookshelf.plug('registry') in case of circular dependency errors.
3) Checking the knex docs. Knex(tablename) is part of the query builder and SHOULD work. So I added,
knex('gods');
To the end of my bookshelf.js file and it threw a similar, [knex is not a function] error.
I've considered that the issue might be that the knex module is being improperly loaded, but I am not sure how to check to see if that is the case. For the moment, I'm kind of stumped.
Apologies if anything above was unclear, I'm still fairly new to node and javascript development and am happy to provide any other information that would help sort this issue out.
Thanks!
Edit:
Knexfile.js:
module.exports = {
client: 'pg',
connection: {
host: '<my cloud host [redacted]>',
user: process.env.DBUSER,
password: process.env.DBPASSWORD,
database: '<my db name [redacted]>'
}
};
This one was tricky. The issue was caused by the .debug(true) knex call, that does not return a knex instance. So change bookshelf.js to
'use strict'
var knex = require('knex')(require('./knexfile'));
knex.debug(true);
var bookshelf = require('bookshelf')(knex);
bookshelf.plugin('registry');
module.exports = bookshelf;
Instead of forcing debug mode this way it could be also better to set it from knex connect string like:
{
client: 'pg',
connection: {
host: '<my cloud host [redacted]>',
user: process.env.DBUSER,
password: process.env.DBPASSWORD,
database: '<my db name [redacted]>'
},
debug: true
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With