mongoskin 查询,在find() 里面我想对数组的元素的dbref做一个db.dereference后,res.render()这个数组应该怎么做....请指教
发布于 12 年前 作者 iceblog 5944 次浏览 最后一次编辑是 8 年前

collection.findItems(query, option, function(err, array){ //这里应该怎么做 // var users = new Array(); // array.forEach(function(doc){ // db.dereference(doc.dbref_role, function(err, d){ // users.push(d); // }); // }); res.render(‘index’, {a:users}) });

4 回复
var _users = new Array();
collection.findItems(query,option,function(err,array){
    //遍历咯
    array.forEach(function(item){
        db.dereference(item.dbref_role,function(err,doc){
            _users.push(doc);
            if(_users.length == array.length){
                res.render('index',{a:_users});
            }
        });
    });
});

不知道是不是你要的答案, 我用的是mongoose,没用过mongoskin,但是异步嵌套的数据处理大都是这类似的处理。

非常感谢!!!!

加个DBRef自动展开的功能怎样?@guilin

应该加一个,要不好麻烦

回到顶部