gpt4 book ai didi

javascript - 引用错误: bookshelf is not defined

转载 作者:行者123 更新时间:2023-11-29 19:10:12 27 4
gpt4 key购买 nike

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

ReferenceError:书架未定义 在 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 ( http://localhost:9876/context.js:151:12 ) 类型错误:无法读取未定义的属性“then” 在对象。 (app_serv/order/test/order.util.spec.js:94074:4)

类型错误:_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});
})
});
}

单元测试代码:

it("addCategoryWithProducts", function(done){
addCategoryWithProducts(categoryMock)
.then(function(catWithPrds){
expect(catWithPrds).toBeDefined();
expect(catWithPrds.products).toBeDefined();
expect(catWithPrds.products.length).toBe(3);
})

);

在尝试了多种解决方案均未成功后,我决定寻求您的帮助。

最佳答案

尚不完全清楚您的代码库是如何工作的,但从 TypeError: Cannot read property 'then' of undefined 看来您没有正确返回单元测试的 promise 。

尝试改变

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

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

您还可以简化您的 promise 和交易处理:

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

关于javascript - 引用错误: bookshelf is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43151557/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com