【问题标题】:async.map not working as expectedasync.map 未按预期工作
【发布时间】:2017-01-23 17:09:12
【问题描述】:

我正在尝试遍历一个集合并从相关集合中附加一个对象数组。我得到的结果不包含该数组。 谁能帮我找出问题所在?

模型关系是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, next){

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
    }
]

我希望返回的对象包含一个数组“问题”,但不知何故它不存在。

提前谢谢你!!

【问题讨论】:

    标签: node.js express mongoose mean-stack async.js


    【解决方案1】:

    也许在您的“路由”代码中,错误的变量传递给了回调。 我认为done(null, pages) 应该是done(null, p)res.status(200).json(pages) 应该是res.status(200).json(result)

    而且由于 mongoose 会忽略架构中不存在的字段,questions 不会出现在您的结果中。 在您的“路线”代码中,p 是一个猫鼬文档,您可以通过 .toObject() 将其转换为纯 JavaScript 对象。

    所以也许你可以用questions 替换你的代码来得到结果:

    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'
    })
    

    【讨论】:

    • 没错。我不知道猫鼬没有返回不属于架构的字段!非常感谢您的宝贵时间! :)
    猜你喜欢
    • 2015-10-09
    • 2021-06-04
    • 2022-01-24
    • 2015-05-11
    • 2020-05-15
    • 2014-10-31
    • 2018-02-12
    • 2014-01-20
    • 2015-01-13
    相关资源
    最近更新 更多