2016-06-26 4 views
0

갤러리에 다른 필드가 들어있는 Project라는 새 개체를 만들었습니다. 보기에서 일부 데이터가 표시되고 이전 및 다음 프로젝트에 대한 링크를 넣으 려합니다. 나는 이미 이전 프로젝트를 얻을 수 있었지만, 슬러그를 얻으려고 할 때 어떻게 든 작동하지 않습니다. 이것은 컨트롤러보기의 개체에서 슬러그 가져 오기가 작동하지 않습니다.

var keystone = require('keystone'); 
var Types = keystone.Field.Types; 

/** 
* Project Model 
* ========== 
*/ 

var Project = new keystone.List('Project', { 
    map: { name: 'title' }, 
    autokey: { path: 'slug', from: 'title', unique: true } 
}); 

Project.add({ 
    title: { type: String, required: true }, 
    state: { type: Types.Select, options: 'draft, published, archived', default: 'draft', index: true }, 
    author: { type: Types.Relationship, ref: 'User', index: true }, 
    publishedDate: { type: Types.Date, index: true, dependsOn: { state: 'published' } }, 
    category: { type: String, required: false }, 
    description: { type: Types.Html, wysiwyg: true, height: 150 }, 
    shortDescription: { type: Types.Html, wysiwyg: true, height: 100 }, 
    credits: { type: Types.Html, wysiwyg: true, height: 100 }, 
    galleries: { type: Types.Relationship, ref: 'Gallery', many: false }, 
    color: { type: String, required: false } 
}); 

Project.schema.virtual('content.full').get(function() { 
    return this.content.extended || this.content.brief; 
}); 

Project.defaultColumns = 'title, state|20%, author|20%, publishedDate|20%'; 
Project.register(); 

:

는 프로젝트 모델입니다

var keystone = require('keystone'); 

exports = module.exports = function(req, res) { 

    var view = new keystone.View(req, res); 
    var locals = res.locals; 

    // Set locals 
    locals.section = 'projects'; 
    locals.filters = { 
     project: req.params.project 
    }; 
    locals.data = { 
     projects: [], 
     previousProject: [] 
    }; 

    // Load the current project 
    view.on('init', function(next) { 

     var q = keystone.list('Project').model.findOne({ 
      state: 'published', 
      slug: locals.filters.project 
     }).populate('galleries'); 

     q.exec(function(err, result) { 
      locals.data.project = result; 
      next(err); 
     }); 

    }); 

    //Load other projects 
    view.on('init', function(next) { 

     var q = keystone.list('Project').model.find({state: "published", publishedDate: {$lt: locals.data.project.publishedDate}}).sort('-publishedDate').limit(1); 
     q.exec(function(err, results) { 
      locals.data.previousProject = results; 
      next(err); 
     }); 

    }); 

    // Render the view 
    view.render('project'); 

}; 

그리고 이것은이다 :

<div class="container"> 
    <p>{{{data.project.title}}}</p> 
    <p>—</p> 
    <p>{{{data.project.category}}}</p> 

    {{#if data.project.galleries}} 
     {{#each data.project.galleries.images}} 
      <img src="{{url}}" /> 
     {{/each}} 
    {{/if}} 
    <p>full project: {{data.previousProject}}</p> 
    <p>slug: {{data.previousProject.slug}}</p> 
    {{#if data.previousProject}} 
     <a href="/projects/{{data.previousProject.slug}}" >Previous project</a> 
    {{/if}} 
</div> 

어떻게 든, {{data.previousProject}} 정확한 기록 정보를 보여줍니다 하지만 내가 할 때 {{data.previousProject.slug}} 전혀 아무것도 반환하지 않습니다. 나는 이것을 몇 시간 동안 긁어 왔지만 문제가 어디인지는 알 수 없다. 미리 감사드립니다 !!

답변

0

마침내 문제가 무엇인지 알게되었습니다. 컨트롤러에서 model.find을 사용하고 있지만 model.findOne을 사용해야하는 이유는 단지 하나의 레코드 만 필요하고 직접적으로 .slug에서 값을 얻고 싶다는 것입니다. limit(1)을 사용하는 것만으로는 충분하지 않았습니다.

관련 문제