我在同时访问多个 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/