未处理的拒绝RangeError:超出最大调用堆栈大小

时间:2016-02-01 15:57:50

标签: bookshelf.js

我收到此错误但不知道原因。有任何想法吗?我确信我有这个工作。我试图引入两个相同的关系类型,其中一个记录有一个格式和一个集合,但集合/格式有很多记录。

错误

[nodemon] starting `node ./bin/www
Unhandled rejection RangeError: Maximum call stack size exceeded
    at /home/ubuntu/workspace/node_modules/bookshelf/node_modules/lodash/index.js:2451:22
    at isArrayLike (/home/ubuntu/workspace/node_modules/bookshelf/node_modules/lodash/index.js:4075:40)
    at keys (/home/ubuntu/workspace/node_modules/bookshelf/node_modules/lodash/index.js:9674:43)
    at baseAssign (/home/ubuntu/workspace/node_modules/bookshelf/node_modules/lodash/index.js:1591:28)
    at /home/ubuntu/workspace/node_modules/bookshelf/node_modules/lodash/index.js:9209:11
    at Function.<anonymous> (/home/ubuntu/workspace/node_modules/bookshelf/node_modules/lodash/index.js:3024:13)
    at Function.<anonymous> (/home/ubuntu/workspace/node_modules/bookshelf/node_modules/lodash/index.js:8152:31)
    at RelationBase (/home/ubuntu/workspace/node_modules/bookshelf/lib/base/relation.js:20:5)
    at child [as constructor] (/home/ubuntu/workspace/node_modules/bookshelf/lib/extend.js:15:14)
    at new child (/home/ubuntu/workspace/node_modules/bookshelf/lib/extend.js:15:14)
    at _relation (/home/ubuntu/workspace/node_modules/bookshelf/lib/bookshelf.js:66:14)
    at belongsTo (/home/ubuntu/workspace/node_modules/bookshelf/lib/model.js:211:17)
    at bookshelf.Model.extend.format (/home/ubuntu/workspace/models/index.js:19:21)
    at init (/home/ubuntu/workspace/node_modules/bookshelf/lib/relation.js:29:31)
    at belongsTo (/home/ubuntu/workspace/node_modules/bookshelf/lib/model.js:211:76)
    at bookshelf.Model.extend.format (/home/ubuntu/workspace/models/index.js:19:21)
    at init (/home/ubuntu/workspace/node_modules/bookshelf/lib/relation.js:29:31)
    at belongsTo (/home/ubuntu/workspace/node_modules/bookshelf/lib/model.js:211:76)
    at bookshelf.Model.extend.format (/home/ubuntu/workspace/models/index.js:19:21)
    at init (/home/ubuntu/workspace/node_modules/bookshelf/lib/relation.js:29:31)
    at belongsTo (/home/ubuntu/workspace/node_modules/bookshelf/lib/model.js:211:76)
    at bookshelf.Model.extend.format (/home/ubuntu/workspace/models/index.js:19:21)
    at init (/home/ubuntu/workspace/node_modules/bookshelf/lib/relation.js:29:31)
    at belongsTo (/home/ubuntu/workspace/node_modules/bookshelf/lib/model.js:211:76)
    at bookshelf.Model.extend.format (/home/ubuntu/workspace/models/index.js:19:21)
    at init (/home/ubuntu/workspace/node_modules/bookshelf/lib/relation.js:29:31)
    at belongsTo (/home/ubuntu/workspace/node_modules/bookshelf/lib/model.js:211:76)
    at bookshelf.Model.extend.format (/home/ubuntu/workspace/models/index.js:19:21)
`

模型

var Collection = bookshelf.Model.extend({
    tableName: 'collections',
    records: function() {
        return this.hasMany(Record);
    }

});

exports.Collection = Collection;

var Record = bookshelf.Model.extend({
    tableName: 'records',
    collection: function() {
        return this.belongsTo(Collection);
    },
    format: function(){
        return this.belongsTo(Format)
    },
    virtuals: {
        largeURL: function() {
            return "https://s3-eu-west-1.amazonaws.com/dartmoorweb/da" + this.get('id') + "_l.jpg";
        },
        mediumURL: function() {
            return "https://s3-eu-west-1.amazonaws.com/dartmoorweb/da" + this.get('id') + "_m.jpg";
        },
        smallURL: function() {
            return "https://s3-eu-west-1.amazonaws.com/dartmoorweb/da" + this.get('id') + "_s.jpg";
        },
        thumbURL: function() {
            return "https://s3-eu-west-1.amazonaws.com/dartmoorweb/da" + this.get('id') + "_t.jpg";
        }
    }
});

exports.Record = Record;

var Format = bookshelf.Model.extend({
    tableName: 'formats',
    records: function() {
        return this.hasMany(Record);
    }
});

exports.Format = Format;

路线

router.get('/:id', function(req, res) {
    models.Record.where('id', req.params.id).fetch({
        withRelated: ['collection','format']
    }).then(function(data) {
        console.log(data.toJSON({virtuals: true}))
        res.render('record/view.html', {
            title: data.name,
            data: data.toJSON({virtuals: true})
        });
    });
});

1 个答案:

答案 0 :(得分:1)

format: function(){
    return this.belongsTo(Format)
},

format()是一个已经在书架的modelBase中定义的方法,你试图将这个方法重写为一个完全不同的目的,因此你有一个无限的递归。

尝试重命名格式方法并修改所有关系