2016-09-15 9 views
0

컬렉션을 루프 처리하고 관련 컬렉션에서 개체 배열을 추가하려고합니다. 내가 얻는 결과는 그 배열을 포함하지 않는다. 누구든지 문제를 파악할 수 있습니까?async.map이 예상대로 작동하지 않습니다.

모델 관계에는 많은 질문이 있습니다.

질문 스키마 :

var mongoose = require('mongoose'), 
    Schema = mongoose.Schema; 

    var questionSchema = new Schema({ 
     _poll     : { type: Schema.Types.ObjectId, ref: 'Poll' }, 
     _page     : { type: Schema.Types.ObjectId, ref: 'Page' }, 
     type     : { type : String, required: true }, 
     title     : { type : String, required: true }, 
     required    : { type : Boolean }, 
     help_text    : { type : String }, 
    }, 
    { timestamps: { createdAt: 'created_at', updatedAt: 'updated_at' } 
    }); 

    mongoose.model('Question', questionSchema); 

페이지 스키마 :

var mongoose = require('mongoose'), 
Schema = mongoose.Schema; 

var pageSchema = new Schema({ 
    _poll    : { type: Schema.Types.ObjectId, ref: 'Poll' }, 
    title    : { type : String, required: true }, 
    intro    : { type : String }, 
    list_style_type : { type : String }, 
}, 
{ timestamps: { createdAt: 'created_at', updatedAt: 'updated_at' } 
}); 

mongoose.model('Page', pageSchema); 

경로

지수 : 기능 (REQ, 고해상도, 다음) {

Poll.findOne({_id: req.params.poll_id}, function(err, poll){ 
    if(err) return next(err); 

    Page.find({_poll: poll.id}, function(err, pages){ 
    if(err) return next(err); 

    async.map(pages, function(p, done) { 
     Question.find({_page: p._id}, function(err, q){ 
     if(err) return next(err); 
     p.questions = q; // <-- SHOULDN'T THIS WORK? // 
     done(null, pages); 
     }); 
    }, function(err, result) { 
     if(err) return next(err); 
     res.status(200).json(pages); 
    }) 

    }); 
}); 

},

결과 나는 점점 오전 :

[ 
    { 
    _id: "57d960fb569dc4101a83525e", 
    updated_at: "2016-09-14T14:38:51.113Z", 
    created_at: "2016-09-14T14:38:51.113Z", 
    _poll: "57c88775992af4c84f99c5d0", 
    title: "test section", 
    list_style_type: "lower-roman", 
    __v: 0 
    }, 
    { 
    _id: "57d9691e22eb81583e20a1c1", 
    updated_at: "2016-09-14T15:13:34.244Z", 
    created_at: "2016-09-14T15:13:34.244Z", 
    _poll: "57c88775992af4c84f99c5d0", 
    title: "this is a new page", 
    list_style_type: "lower-roman", 
    intro: "jkc hcsad", 
    __v: 0 
    }, 
    { 
    _id: "57d9a97d1e7863b81f9e4d0e", 
    updated_at: "2016-09-14T19:48:13.816Z", 
    created_at: "2016-09-14T19:48:13.816Z", 
    _poll: "57c88775992af4c84f99c5d0", 
    title: "Consequatur Vero necessitatibus consequatur hic", 
    list_style_type: "upper-latin", 
    intro: "Rem laboris est omnis ducimus, minim autem itaque minim dolore ea odio aliqua. Autem qui id, sit nulla id.", 
    __v: 0 
    }, 
    { 
    _id: "57dab7d7f54387d41d976614", 
    updated_at: "2016-09-15T15:01:44.001Z", 
    created_at: "2016-09-15T15:01:44.001Z", 
    _poll: "57c88775992af4c84f99c5d0", 
    title: "This is a new page", 
    list_style_type: "lower-roman", 
    intro: "cjksahc dsa", 
    __v: 0 
    } 
] 

가 나는 배열 "질문에"포함 반환 된 개체를 예상하지만, 어떻게 든이 없습니다.

미리 감사드립니다.

답변

0

"Route"코드에 잘못된 변수가 콜백에 전달되었을 수 있습니다. done(null, pages)done(null, p)이어야하며 res.status(200).json(pages)res.status(200).json(result)이어야합니다.

몽구스가 스키마에 존재하지 않는 필드를 무시하기 때문에 questions이 결과에 나타나지 않습니다. "Route"코드에서 p은 몽구스 문서이므로 .toObject()으로 일반 자바 스크립트 개체로 변환 할 수 있습니다. 올

async.map(pages, function(p, done) { 
    Question.find({_page: p._id}, function(err, q){ 
    if(err) return next(err); 
    p = p.toObject(); // convert a mongoose document into a javascript object 
    p.questions = q; 
    done(null, p); // modified 'pages' -> 'p' 
    }); 
}, function(err, result) { 
    if(err) return next(err); 
    res.status(200).json(result); // modified 'pages' -> 'result' 
}) 
+0

:

그래서 어쩌면 당신은 같은 코드를 대체하여 questions와 결과를 얻을 수 있습니다. 몽구스가 스키마의 일부가 아닌 필드를 반환하지 않았다는 것을 나는 몰랐다! 시간 내 주셔서 대단히 감사합니다! :) – rsilva

관련 문제