2016-09-15 51 views
0

我想循環一個集合並追加一個相關集合中的對象數組。我得到的結果不包含該數組。 任何人都可以幫我找出問題嗎?async.map不按預期方式工作

模型關係是Page有很多問題。

問題架構:

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,RES,下一個){

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

也許在你的「路由」代碼中,錯誤的變量傳遞給了回調函數。我認爲done(null, pages)應該是done(null, p)res.status(200).json(pages)應該是res.status(200).json(result)

而且由於貓鼬會忽略模式中不存在的字段,因此questions不會出現在您的結果中。 在你的「路由」代碼中,p是一個貓鼬文檔,你可以通過.toObject()將它轉換成一個普通的javascript對象。

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