rhwilr/adonis-bumblebee

A transformer must be a function or a class extending TransformerAbstract

FelipeFernandesLeandro opened this issue · 1 comments

When I try to use any of the following: "ProductController.index" or "CategoryController.show" everything works as expected. But after making the first one, at the next request I get the following error message: "A transformer must be a function or class extending TransformerAbstract" .

Below is the summary of the classes that I am using.
 

class Product extends Model {
    categories() {
        return this.belongsToMany('App/Models/Category', 'product_id', 'category_id').pivotTable('product_categories')
    }
}
class Category extends Model {
    products() {
        return this.belongsToMany('App/Models/Product', 'category_id', 'product_id').pivotTable('product_categories')
    }
}
class ProductCategorySchema extends Schema {
	up() {
		this.create('product_categories', table => {
			table.increments()
			table
				.integer('category_id')
				.unsigned()
				.references('id')
				.inTable('categories')
				.onDelete('cascade')
			table
				.integer('product_id')
				.unsigned()
				.references('id')
				.inTable('products')
				.onDelete('cascade')
		})
	}
}
class ProductController {
    async index({ request, response, pagination, transform }) {
        const query = Product.query()
        let products = await query.paginate(pagination.page, pagination.limit)
        products = await transform.include('categories').paginate(products, Transformer)
        return response.send(products)
    }
}
class CategoryController {
	async show({ params: { id }, response, transform }) {
		let category = await Category.findOrFail(id)
		category = await transform.include('products').item(category, Transformer)
		return response.send(category)
	}
}
class ProductTransformer extends BumblebeeTransformer {
	static get availableInclude() {
		return ['categories']
	}
	includeCategories(model) {
		return this.collection(model.getRelated('categories'), CategoryTransformer)
	}
}
class CategoryTransformer extends BumblebeeTransformer {
	static get availableInclude() {
		return ['products']
	}
	includeProducts(model) {
		return this.collection(model.getRelated('products'), ProductTransformer)
	}
}

Finally I was able to fix it.
The problem was how the transformers classes was being called. At first I was doing:

const CategoryTransformer = use('App/Transformer/CategoryTransformer ')
...
category = await transform.include('products').item(category, CategoryTransformer)

but since I changed to just invoke the transformer like:

category = await transform.include('products').item(category, 'CategoryTransformer')

It works fine.