我使用的是keystone@0.2.32。我想将帖子类别更改为树结构。下面的代码运行良好,除非我创建一个类别,它会陷入僵局:
var keystone = require('keystone'),
Types = keystone.Field.Types;
/**
* PostCategory Model
* ==================
*/
var PostCategory = new keystone.List('PostCategory', {
autokey: { from: 'name', path: 'key', unique: true }
});
PostCategory.add({
name: { type: String, required: true },
parent: { type: Types.Relationship, ref: 'PostCategory' },
parentTree: { type: Types.Relationship, ref: 'PostCategory', many: true }
});
PostCategory.relationship({ ref: 'Post', path: 'categories' });
PostCategory.scanTree = function(item, obj, done) {
if(item.parent){
PostCategory.model.find().where('_id', item.parent).exec(function(err, cats) {
if(cats.length){
obj.parentTree.push(cats[0]);
PostCategory.scanTree(cats[0], obj, done);
}
});
}else{
done();
}
}
PostCategory.schema.pre('save', true, function (next, done) { //Parallel middleware, waiting done to be call
if (this.isModified('parent')) {
this.parentTree = [];
if(this.parent != null){
this.parentTree.push(this.parent);
PostCategory.scanTree(this, this, done);
}else
process.nextTick(done);
}else
process.nextTick(done); //here is deadlock.
next();
});
PostCategory.defaultColumns = 'name, parentTree';
PostCategory.register();

非常感谢。
答案 0 :(得分:1)
正如我在此处对您登录Keystone的问题进行了解释:https://github.com/keystonejs/keystone/issues/759
这似乎是猫鼬中可重现的错误,可防止中间件在以下情况下解析:
将Keystone的autokey
中间件更改为以并行模式运行可能会导致其他用例出现错误,因此无法完成。答案是以串行模式而不是并行模式实现parentTree中间件。
另外,我注意到了其他一些事情:
scanTree
方法作为schama上的方法更好地实现findById
方法进行更简单的父查询架构方法如下所示:
PostCategory.schema.methods.addParents = function(target, done) {
if (this.parent) {
PostCategory.model.findById(this.parent, function(err, parent) {
if (parent) {
target.parentTree.push(parent.id);
parent.addParents(target, done);
}
});
} else {
done();
}
}
固定的中间件看起来像这样:
PostCategory.schema.pre('save', function(done) {
if (this.isModified('parent')) {
this.parentTree = [];
if (this.parent != null) {
PostCategory.scanTree(this, this, done);
} else {
process.nextTick(done);
}
} else {
process.nextTick(done);
}
});
答案 1 :(得分:0)
我认为这是keystone.js的错误。我已经更改了schemaPlugins.js 104行
这
this.schema.pre('save', function(next) {
到
this.schema.pre('save', true, function(next, done) {
并从第124行更改为以下内容,
// if has a value and is unmodified or fixed, don't update it
if ((!modified || autokey.fixed) && this.get(autokey.path)) {
process.nextTick(done);
return next();
}
var newKey = utils.slug(values.join(' ')) || this.id;
if (autokey.unique) {
r = getUniqueKey(this, newKey, done);
next();
return r;
} else {
this.set(autokey.path, newKey);
process.nextTick(done);
return next();
}

有效。