builder = bookshelf.knex(tableNameOrBuilder);

builder = bookshelf.knex(tableNameOrBuilder);

我是 node.js 和 ORM 的新手,我正在使用 bookshelf 进行数据库查询。 因此,我检查了 bookshelf 的文档,当我尝试在 mysql 中构建对现有模式的查询时,它抛出以下错误:

    TypeError: bookshelf.knex is not a function
        at Child.builderFn [as _builder] (/home/mauricio/techday/techday-            node/node_modules/bookshelf/lib/bookshelf.js:314:27)
at Object.query (/home/mauricio/techday/techday-node/node_modules/bookshelf/lib/helpers.js:44:23)
at Child.query (/home/mauricio/techday/techday-node/node_modules/bookshelf/lib/model.js:1243:30)
at Function.Model.(anonymous function).Collection.(anonymous function) [as query] (/home/mauricio/techday/techday-node/node_modules/bookshelf/lib/bookshelf.js:333:28)
at Object.<anonymous> (/home/mauricio/techday/techday-node/app.js:20:2)
at Module._compile (module.js:571:32)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:427:7)
at startup (bootstrap_node.js:151:9)
at bootstrap_node.js:542:3

这是我的查询:

    user
       .query('where', 'usuario', '=', 'admin')
       .fetch()
       .then(function(user) {
        console.log(user) 
    });

这是我的 db.js:

   'use strict'
       var db = require('bookshelf')(knex);
       var securePassword = require ('bookshelf-secure-password');
       db.plugin(securePassword);
       db.plugin('registry');


       var knex = require('knex')({
         client: 'mysql',
         connection: {
         host: 'localhost',
         user: 'root',
         password: 'password',
         database: 'test',
         charset: 'utf8'
       }
     });




       module.exports = db;

现在我的 user.js

var db = require('../Commons/db.js');
var bcrypt = require('bcrypt-nodejs');

var user = db.Model.extend({
    tableName: 'usuario',
    hasSecurePassword: 'contraseña'
});



 module.exports = db.model('user',user);

我不太了解书架,但您的 db.js 代码至少有一个问题是您正在使用 undefined knex 初始化书架。

var db = require('bookshelf')(knex);
// knex is still undefined and will be initialized later on
// when you do var knex = require('knex')({...})

那里不会抛出错误,因为 javascript var变量从函数开始就被提升为始终存在,即使它们未初始化也是如此。

如果您使用let声明变量,节点会在您尝试初始化书架时抛出一些未知变量错误。

连接的定义必须在创建书架实例之前进行,因此将 db.js 更改为:

'use strict';
var knex = require('knex')({
  client: 'mysql',
  connection: {
    host: 'localhost',
    user: 'root',
    password: 'password',
    database: 'test',
    charset: 'utf8'
  }
});
var db = require('bookshelf')(knex); // now bookshelf knows how to connect
var securePassword = require('bookshelf-secure-password');
db.plugin(securePassword);
db.plugin('registry');

module.exports = db;

无论如何,不​​建议将数据库连接显式放置在代码中。对于基于 Knex 的数据库(如 Bookshelf),更好的方法应该是使用 knexfile,这样可以更轻松地使用 knex CLI(迁移、种子)。

在这种情况下,您的代码如下所示:

'use strict';
var knex = require('knex')(
  require('./knexfile')[process.env.NODE_ENV||'development']);
var db = require('bookshelf')(knex);
var securePassword = require('bookshelf-secure-password');
db.plugin(securePassword);
db.plugin('registry');

module.exports = db;