简体   繁体   English

Bookshelf.knex不是函数错误

[英]Bookshelf.knex is not a function error

Having some trouble with integrating Bookshelf and Knex into my stack. 将Bookshelf和Knex集成到我的堆栈中有些麻烦。 When attempting to perform db read/write, I get a TypeError: knex is not a function. 尝试执行db read / write时,我得到一个TypeError:knex不是函数。

My bookshelf.js: 我的书架.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: 我的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_mod TypeError:bookshelf.knex不是在Object.query(D:\\ Repositories \\ knextest \\ node_modules)的builderFn [as _builder](D:\\ Repositories \\ knextest \\ node_modules \\ bookshelf \\ lib \\ bookshelf.js:314:27)中的函数\\ bookshelf \\ lib \\ helpers.js:44:23)查询(D:\\ Repositories \\ knextest \\ node_modules \\ bookshelf \\ lib \\ model.js:1243:30)at(D:\\ Repositories \\ knextest \\ node_modules \\ bookshelf) \\ lib \\ model.js:1278:23)在Function.Model。(匿名函数).Collection。(匿名函数)[如何](D:\\ Repositories \\ knextest \\ node_modules \\ bookshelf \\ lib \\ bookshelf.js:333 :28)在Layer.handle的validate_key_secret(D:\\ Repositories \\ knextest \\ app \\ controllers \\ gods.server.controller.js:114:3)[作为handle_request](D:\\ Repositories \\ knextest \\ node_modules \\ express \\ lib) \\ router \\ layer.js:95:5)在Route.dispatch的下一个(D:\\ Repositories \\ knextest \\ node_modules \\ express \\ lib \\ router \\ route.js:131:13)(D:\\ Repositories \\ knextest \\ node_modules) \\ express \\ lib \\ router \\ route.js:112:3)在Layer.handle [as handle_request](D:\\ Repositories \\ knextest \\ node_mod ules\\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) ules \\ express \\ lib \\ router \\ layer.js:95:5)在D:\\ Repositories \\ knextest \\ node_modules \\ express \\ lib \\ router \\ index.js:277:22 at Function.process_params(D:\\ Repositories \\ knextest)在SessionStrategy.strategy.pass上的\\ node_modules \\ express \\ lib \\ router \\ index.js:330:12)下一步(D:\\ Repositories \\ knextest \\ node_modules \\ express \\ lib \\ router \\ index.js:271:10) D:\\ Repositories \\ knextest \\ node_modules \\ passport \\ lib \\ middleware \\ authenticate.js:325:9)在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. 1)调查bookshelf.js节点模块。 The error is occurring in ./node_modules/bookshelf/lib/bookshelf.js line 314: 该错误发生在./node_modules/bookshelf/lib/bookshelf.js第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. 我能够执行简单的SQL读写操作。 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. 但是,这样做确实排除了我的knexfile / db配置可能出现的任何问题。

2) Integrating bookshelf.plug('registry') in case of circular dependency errors. 2)在循环依赖性错误的情况下集成bookshelf.plug('registry')

3) Checking the knex docs. 3)检查knex文档。 Knex(tablename) is part of the query builder and SHOULD work. Knex(tablename)是查询构建器的一部分,应该工作。 So I added, 所以我补充说,

knex('gods');

To the end of my bookshelf.js file and it threw a similar, [knex is not a function] error. 到我的bookshelf.js文件的末尾,它抛出了类似的,[knex不是函数]错误。

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. 我认为问题可能是knex模块加载不正确,但我不知道如何检查是否是这种情况。 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. 抱歉,如果上面的任何内容不清楚,我仍然是节点和javascript开发的新手,我很乐意提供任何其他有助于解决这个问题的信息。

Thanks! 谢谢!

Edit: 编辑:

Knexfile.js: 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. 该问题是由.debug(true) knex调用引起的,该调用返回knex实例。 So change bookshelf.js to 所以将bookshelf.js改为

'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: 而不是以这种方式强制调试模式,也可以更好地从knex连接字符串设置它,如:

{ 
  client: 'pg', 
  connection: { 
    host: '<my cloud host [redacted]>', 
    user: process.env.DBUSER, 
    password: process.env.DBPASSWORD, 
    database: '<my db name [redacted]>' 
  },
  debug: true
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM