Bookshelf.jsregistry
我试图使用Bookshelf.js的registry插件,因为我认为我遇到了循环依赖,但我没有得到它。
在我得到这个错误之前,我得到一个错误:
[Error: A valid target model must be defined for the category hasMany relation]
我有一个类别和标签模型,但我不断收到错误
[Error: The model Tag could not be resolved from the registry plugin.]
我的类别模型如下所示:
var knex = require('knex')(config.knex); var bookshelf = require('bookshelf')(knex); bookshelf.plugin('registry'); var Category = bookshelf.Model.extend({ tableName: 'category', tag: function() { return this.hasMany('Tag', 'category_id'); } }); module.exports = bookshelf.model('Category', Category);
和我的标签模型看起来像
var knex = require('knex')(config.knex); var bookshelf = require('bookshelf')(knex); bookshelf.plugin('registry') var Tag = bookshelf.Model.extend({ tableName: 'tag', category: function() { return this.belongsTo('Category'); }, events: function() { return this.belongsToMany(Event, 'event_tag'); }, }); module.exports = bookshelf.model('Tag', Tag);
我不知道我哪里错了,这是如此令人沮丧。 任何人都可以帮忙吗?
this.hasMany使用表名称或Bookshelf对象名称。 你想使用'tag'
(表名)或Tag
(书架对象名称),而不是'Tag'
(不是一个表名称)。
所以使用这个:
tag: function() { return this.hasMany('tag', 'category_id'); }
或这个 :
tag: function() { return this.hasMany(Tag, 'category_id'); }
您的Tag
模型中会出现同样的问题:
category: function() { return this.belongsTo(Category); // (or 'category') },