我正在尝试将注册表插件与 Bookshelf.js 一起使用,因为我认为我遇到了循环依赖,但我却一无所获。
在出现此错误之前,我遇到了错误:
[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
(Bookshelf 对象名称),但不要使用 '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')
},
关于node.js - Bookshelf.js 注册表 [错误 : The model . .. 无法从注册表插件解析。],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36671596/