2016-12-14 80 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 
    } 
}); 
相關問題