将模型“user”建立为“user_flag”到“user”

时间:2013-11-17 15:44:23

标签: node.js bookshelf.js

我有两种模式:

用户

'use strict';

var Mystical = require('../mystical'),
    Blog     = require('./blog'),
    Flag     = require('./user/flag');

module.exports = Mystical.db.Model.extend({
    tableName: 'user',
    defaults: {
        isAdmin: 0,
        isConfirmed: 0
    },

    blogs: function blogs() {
        return this.hasMany(Blog, 'userId');
    },

    flags: function flags() {
        return this.hasMany(Flag, 'userId');
    }

});

...和举报

'use strict';

var Mystical = require('../../mystical'),
    User     = require('../../model/user');

module.exports = Mystical.db.Model.extend({
    tableName: 'user_flag',

    user: function user() {
        return this.belongsTo(User, 'id');
    }

});

在页面上,我尝试获得一个标志:

(new Flag({ content: req.params.identifier, flag: 'register_complete' })).fetch({ withRelated: ['user'] }).then(onData).otherwise(function(error) {
    console.log(error);
});

但是每当我调用此函数时,否则会触发回调:

TypeError: object is not a function
    at exports.Relation.RelationBase.extend.relatedInstance (/my/secret/dir/node_modules/bookshelf/dialects/sql/relation.js:217:29)
    at exports.Relation.RelationBase.extend.init (/my/secret/dir/node_modules/bookshelf/dialects/sql/relation.js:43:39)
    at exports.Model.ModelBase.extend.belongsTo (/my/secret/dir/node_modules/bookshelf/dialects/sql/model.js:37:76)
    at user (/my/secret/dir/core/model/user/flag.js:10:21)
    at EagerBase.fetch (/my/secret/dir/node_modules/bookshelf/dialects/base/eager.js:56:40)
    at /my/secret/dir/node_modules/bookshelf/dialects/sql/model.js:232:60
    at NearFulfilledProxy.when (/my/secret/dir/node_modules/bookshelf/node_modules/when/when.js:465:43)
    at Object._message (/my/secret/dir/node_modules/bookshelf/node_modules/when/when.js:389:25)
    at deliver (/my/secret/dir/node_modules/bookshelf/node_modules/when/when.js:299:7)
    at /my/secret/dir/node_modules/bookshelf/node_modules/when/when.js:296:63

现在我试图生成与hasMany,hasOne,belongsTo以及belongsToMany的关联。但没有任何作用......

有人有想法吗?

1 个答案:

答案 0 :(得分:1)

我觉得这可能是一个循环引用问题:

尝试更换:

return this.hasMany(Flag, 'userId');

return this.hasMany(require('./user/flag'), 'userId');

return this.belongsTo(User, 'id');

return this.belongsTo(require('../../model/user'), 'id');

看看你是否还有同样的问题。