Javascript ReferenceError:未定义书架

Javascript ReferenceError:未定义书架,javascript,mysql,webpack,karma-jasmine,bookshelf.js,Javascript,Mysql,Webpack,Karma Jasmine,Bookshelf.js,我正在使用karma jasmine,WebAct运行单元测试。 我尝试使用bookshelf在mysql数据库中插入一条记录。 我收到以下错误消息: 参考错误:未定义书架 位于module.exports.addCategoryWithProducts(app_serv/order/test/order.util.spec.js:94215:2) 反对。(app_serv/order/test/order.util.spec.js:94059:3) 在ContextKarma.loaded()

我正在使用karma jasmine,WebAct运行单元测试。 我尝试使用bookshelf在mysql数据库中插入一条记录。 我收到以下错误消息:

参考错误:未定义书架 位于module.exports.addCategoryWithProducts(app_serv/order/test/order.util.spec.js:94215:2) 反对。(app_serv/order/test/order.util.spec.js:94059:3) 在ContextKarma.loaded()中 TypeError:无法读取未定义的属性“then” 反对。(app_serv/order/test/order.util.spec.js:94074:4)

TypeError:\u this.driver.createConnection不是一个函数

我的代码:

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

module.exports = require('bookshelf')(knex);

module.exports.addCategoryWithProducts = function(categoryToAdd) {

  bookshelf.transaction(function(t) {
    var products = categoryToAdd.products;
    var tableProd = [];

    //if (!req.params.idCategory || req.params.idCategory ==0) {
    Category.forge({
      name: categoryToAdd.name,
      code: categoryToAdd.code,
      tax_percentage: categoryToAdd.tax_percentage,
      description: categoryToAdd.description
    })
      .save(null, { transacting: t })
      .then(function(category) {
        for (var i = 0; i < products.length; i++) {
          tableProd.push({
            barcode: products[ i ].barcode,
            cip_13: products[ i ].cip_13,
            category_id: category.id,
            quantity: products[ i ].quantity,
            retail_price: products[ i ].retail_price,
            description: products[ i ].description,
            name: products[ i ].name,
            inn: products[ i ].inn,
            kind: products[ i ].kind,
            comment: products[ i ].comment,
            status: products[ i ].status,
            critical_threshold: products[ i ].critical_threshold,
            max_threshold: products[ i ].max_threshold,
            date_end: products[ i ].date_end
          });
        }

        Products.forge(tableProd)
          .invokeThen('save', null, { transacting: t })
          .then(function(products) {


            //console.log('inside insert of collection '+ JSON.stringify({category : category.toJSON(), produits: products}))
            //res.json({error : false, data: {category : category.toJSON(), products: products}});

            t.commit();

            return Promise.resolve(category.related('products'));
          })
          .catch(function(err) {
            return Promise.reject(err.message);
            //console.log('Erreur dans  Products.forge: ' + err.message);
            res.status(500)
              .json({ error: true, code: err.code, message: err.message });
          })

      })
      .catch(function(err) {
        return Promise.reject(err.message)
        //console.log('Erreur dans  Products.forge: ' + err.message);
        //res.status(500).json({error: true, code : err.code, message: err.message});
      })
  });
}

在尝试了许多解决方案都失败后,我决定寻求您的帮助。

目前还不完全清楚您的代码库是如何工作的,但是从
TypeError:cannotread属性'then'的undefined
来看,您似乎没有正确地返回对单元测试的承诺

试着改变

Products.forge(tableProd)
  .invokeThen('save', null, { transacting: t })
  .then(function(products) {

此外,您还可以简化您的承诺和交易处理:

  • 您不需要显式调用
    t.commit()
    ,因为如果承诺得到解决,它会被Bookshelf自动调用
  • 您通常不需要在另一个承诺中使用承诺。因此,您可以使用
    返回category.related('products')
    而不是
    返回Promise.reject(err.message)
    just
    返回err.message
    (但重新抛出可能更好)

您的代码看起来不完整。
书架在哪里以及如何定义?它看起来像是
require('bookshelf')(knex)之前的代码属于另一个源文件。是的,它位于另一个文件中。我认为问题不存在。也许不是,但您的错误明确提到未定义
bookshelf
。因此,人们首先想到的是它的定义在哪里以及如何定义。如果正确分割代码段,可能会有所帮助。我尝试更改代码,但仍有相同的错误消息。我还根据您的建议简化了我的代码,我想感谢您,您的意思是您仍然得到未定义的
错误的
TypeError:cannotread属性'then'?因为这是我认为我有足够数据的唯一一个。
ReferenceError:bookshelf未定义
TypeError:\u this.driver.createConnection
错误似乎与如何定义
bookshelf
引用有关。
Products.forge(tableProd)
  .invokeThen('save', null, { transacting: t })
  .then(function(products) {
return Products.forge(tableProd)
  .invokeThen('save', null, { transacting: t })
  .then(function(products) {