node.js - Sails Js - 同时访问多个 Controller

标签 node.js sails.js

我在同时访问多个 Controller 时遇到问题, 例如,当“访问”处于事件状态时,我正在访问方法“访问”,我无法在客户端使用/访问方法“其他”或其他 Controller , 但是当“访问”中的循环完成后,我可以使用其他方法或 Controller ,SailsJs Controller 是单线程吗?

access: function (req, res) {
  // Assume that I'll generate 1k data and I dont have problem about that
  // my problem is while generating 1k data i cant access my other Controller/Method
  // any solution about my problem thanks :) 
  // NOTE** this is just a example of the flow of my program
  // In creating data Im using Async

  while(x <= 1000) {
    Model.create(etc, function (err, ok) {
      if(err) console.log(err)
    });
    x++;
  }

  res.view('view/sampleview');
},

other: function (req, res) {
  res.view('view/view');
},

最佳答案

所有 Controller 和 Action 都在 sails.contollers variavel Mike sails.controllers.mycontroller.access (req, res) 中可用;

同时并行运行:

access: function (req, res) {
  var createFunctions = [];  

  while(x <= 1000) {
    createFunctions.push(function(done) {
      Model.create(etc).exec(function (err, ok) {
        if(err) return done(err); // err
        done(); //success
      });
    })

    x++;
  }

  async.parallel( createFunctions, function afterAll(err) {
    sails.controllers.mycontroller.other (req, res);

    //res.view('view/sampleview');
  });
},

other: function (req, res) {
  res.view('view/view');
},

逐一运行:

access: function (req, res) {
  var createFunctions = [];  

  while(x <= 1000) {
    createFunctions.push(function(done) {
      Model.create(etc).exec(function (err, ok) {
        if(err) return done(err); // err
        done(); //success
      });
    })

    x++;
  }

  // run in series, one by one
  async.series( createFunctions, function afterAll(err) {
    sails.controllers.mycontroller.other (req, res);

    //res.view('view/sampleview');
  });
},

other: function (req, res) {
  res.view('view/view');
},

关于node.js - Sails Js - 同时访问多个 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31799706/

相关文章:

php - Laravel5 (PHP) 还是 SailsJS (node.js)?

javascript - Uncaught Error : [$injector:modulerr] Failed to instantiate module toastr

node.js - sails : disable `blueprints actions` in production, 因为它创造了巨大的安全足迹?

angularjs - Node 调用异常 : Attempt to connect to Node timed out

javascript - 当我在 Nodejs+Express 中使用 Passport 进行用户身份验证时,发生了奇怪的事情

Node.JS GET/参数

mysql - ER_NOT_SUPPORTED_AUTH_MODE - sails 1.0 和 MySQL 8.0

sails.js - 如何使用sails js禁用cookies

mysql - 如何将 JWT token 发送到 Node.js 中的 header

node.js https没有响应 'end'事件, 'close'代替?