2016-12-14 55 views
1

我正在过滤我的子部分选择以仅显示与当前mainNavigationSection相关的子部分。这些小节中的每一个都有一个mainNavigation部分。由于某种原因,当前的实现不会返回任何结果。对自己模型中的相关字段进行Keystone.js过滤

这里是我的网页型号:

Page.add({ 
    name: { type: String, required: true }, 
    mainNavigationSection: { type: Types.Relationship, ref: 'NavItem', refPath: 'key', many: true, index: true }, 
    subSection: { type: Types.Relationship, ref: 'SubSection', filters: { mainNavigationSection:':mainNavigationSection' }, many: true, index: true, note: 'lorem ipsum' }, 
    state: { type: Types.Select, options: 'draft, published, archived', default: 'draft', index: true }, 
    author: { type: Types.Relationship, ref: 'User', index: true } 
} 

这里是我的subSectionModel:

SubSection.add({ 
    name: { type: String, required: true, index: true }, 
    mainNavigationSection: { type: Types.Relationship, ref: 'NavItem', many: true, required: true, initial: true}, 
    showInFooterNav: { type: Boolean, default: false }, 
    defaultPage: { type: Types.Relationship, ref: 'Page' }, 
    description: { type: Types.Html, wysiwyg: true, height: 150, hint: 'optional description' } 
}); 

回答

0

从它看来,你有很多mainNavigationSections你的模型的可能性。您必须在当前的Page上迭代它们中的每一个,并找到相关的SubSections。您需要使用async Node module来运行所有查询并从每个查询中获取结果。

var async = require('async'); 
var pID = req.params.pid; // Or however you are identifying the current page 
keystone.list('Page').model.findOne({page: pID}).exec(function (err, page) { 
    if (page && !err) { 
     async.each(page.mainNavigationSection, function (curMainNavigationSection, cb) { 
      keystone.list('SubSection').model 
      .find({mainNavigationSection: curMainNavigationSection._id.toString()}) 
      .exec(function (err2, curSubSections) { 
       if (curSubSections.length !== 0 && !err2) { 
        // Do what you need to do with the navigation subSections here 
        // I recommend using a local variable, which will persist through 
        // every iteration of this loop and into the callback function in order 
        // to persist data 
        return cb(null) 
       } 
       else { 
        return cb(err || "An unexpected error occurred."); 
       } 
      }); 
     }, function (err) { 
      if (!err) { 
       return next(); // Or do whatever 
      } 
      else { 
       // Handle error 
      } 
     }); 
    } 
    else { 
     // There were no pages or you have an error loading them 
    } 
}); 
相关问题