因为我可以在同一个控制器动作中使用不同的模型

时间:2014-06-16 23:17:20

标签: node.js sails.js waterline sails-mongo

我试图向视图发送来自多个模型的信息,如下所示:

var tareasGlob = "";
var directoresProy = "";
var usuariosGlob = "";
var proyectosGlob = "";

module.exports = {


'index' : function(req, res, next){

    // De esta forma se utlisaria la funcion de utilidad que cree en el archivo /api/services/utility.js
    // utility.sliceIt();


    Tarea.find().done(function(err, tareas){
        if(err){ return res.serverError(err); } 
        tareasGlob = tareas; 
    });
    console.log('tareas', tareasGlob);
    DirectorProy.find().done(function(err, directsproy){
        if(err){ return res.serverError(err); } 
        directoresProy = directsproy;
    });

    Usuario.find().done(function(err, usuarios){
        if(err){ return res.serverError(err); } 
        usuariosGlob = usuarios;
    });

    Proyecto.find().done(function(err, proyectos){
        if(err){ return res.serverError(err); } 
        proyectosGlob = proyectos;
    });



    res.view({
        'tareas'         : tareasGlob,
        'directoresproy' : directoresProy,
        'usuarios'       : usuariosGlob,
        'proyectos'      : proyectosGlob
    });

},

然后出现错误,因为当我执行" res.view()"还没有为变量赋值,并且是空的。

在此先感谢您的帮助,您可以帮我解决问题。

2 个答案:

答案 0 :(得分:2)

有一些节点包可以帮助您解决像这样的异步编码问题。 Sails全局化async库,这将允许您将代码重写为:

// All four of the functions inside `async.auto` will run in parallel,
// and if successful their results will be passed to the "allDone"
// function
async.auto({
    tareas: function(cb) {
        Tarea.find().exec(cb);
    },
    directsproy: function(cb) {
        DirectorProy.find().exec(cb);
    },
    usuarios: function(cb) {
        Usuario.find().exec(cb);
    },
    proyectos: function(cb) {
        Proyecto.find().exec(cb);
    }
}, function allDone(err, results) {
    // If any of the functions called the callback with an error,
    // allDone will immediately be called with the error as the
    // first argument
    if (err) {return res.serverError(err);}

    // Otherwise, `results` will be an object whose keys are the
    // same as those of the first argument (tareas, directsproy,
    // usuarios and proyectos, and whose values are the results
    // of their `find` queries.
    res.view(results);
});

您还可以使用async.auto声明不同函数之间的依赖关系,使用async.forEach对数组进行异步循环,以及各种有趣的东西。

async here的完整文档。

答案 1 :(得分:0)

在调用res.view之前,您必须确保所有4个查找方法都已完成。我说有一个计数器可以跟踪你收到的响应数量,每次响应都会增加1。然后将res.view移动到一个方法中,该方法检查var == 4,如果它没有返回。

类似的东西(可能是下面的错误):

'index' : function(req, res, next){
  var counter = 0;

// De esta forma se utlisaria la funcion de utilidad que cree en el archivo /api/services/utility.js
// utility.sliceIt();


Tarea.find().done(function(err, tareas){
    if(err){ return res.serverError(err); } 
    tareasGlob = tareas; 
    counter++;
    this.returnView(res);
});
console.log('tareas', tareasGlob);
DirectorProy.find().done(function(err, directsproy){
    if(err){ return res.serverError(err); } 
    directoresProy = directsproy;
    counter++;
    this.returnView(res);
});

Usuario.find().done(function(err, usuarios){
    if(err){ return res.serverError(err); } 
    usuariosGlob = usuarios;
    counter++;
    this.returnView(res);
});

Proyecto.find().done(function(err, proyectos){
    if(err){ return res.serverError(err); } 
    proyectosGlob = proyectos;
    counter++;
    this.returnView(res);
});



res.view({
    'tareas'         : tareasGlob,
    'directoresproy' : directoresProy,
    'usuarios'       : usuariosGlob,
    'proyectos'      : proyectosGlob
});

},

returnView: function(res){
    if (counter < 4)
      return;
// return view logic here

}
相关问题