如何从 sails.js 上的不同操作获取返回的对象?
How to get returned object from different action on sails.js?
我还是 sails.js 的新手,目前正在尝试从不同的操作中获取 returned 对象。
目前我所知道的是,如果我在 'UserController' 中使用 'getUserTransaction' 操作,那么我只能 return 它到 'getUserTransaction.ejs' 使用当地人查看(_.each). (CMIIW)
示例:
//UserController
getUserTransaction: function (req, res, next){
UserTransaction.find({user:req.param('userid')}).exec(function foundTransactions(err, transactions){
if(err) return next(err);
if(!transactions) return next();
res.view({
transactions: transactions
});
});
}
//getUserTransaction.ejs
...
<% _.each(transactions, function(transaction){ %>
<tr>
<td><%= transaction.data %></td>
<td><a href="<%= transaction.path %>">Link</a></td>
</tr>
<% }); %>
...
但是,如果我想 return 从 'getUserHobby' 操作内部 'UserController' 对象到 'getUserTransaction.ejs' 视图怎么办?。
这是我的代码,但我做错了
//UserController
getUserHobby: function (req, res, next){
UserHobby.find({user:req.param('userid')}).exec(function foundHobbies(err, hobbies){
if(err) return next(err);
if(!hobbies) return next();
res.view('user/getUserTransaction', {
hobbies: hobbies
});
});
}
//getUserTransaction.ejs
...
<% _.each(hobbies, function(hobby){ %>
<tr>
<td><%= hobby.type %></td>
<td><%= hobby.name %></td>
</tr>
<% }); %>
...
我已经尝试这样做并且 returned 'hobbies undefined'。那么我该如何正确处理呢。
此致
首先,在 Sails controller 操作中,您不应使用第三个参数 (next
)。
Unlike Express middleware methods, Sails controller actions should always be the last stop in the request chain--that is, they should always result in either a response or an error. While it is possible to use next
in an action method, you are strongly encouraged to use policies instead wherever possible.
你应该使用内置的response方法即使你想抛出一个错误。
以下是我编写控制器操作的方式:
// Inside api/controllers/UserController.js
getUserTransactions: function (req, res) {
if(!req.param('userid'))
return res.badRequest(); // Send a 400 response back down to the client indicating that the request is invalid
UserTransaction
.find({ user: req.param('userid') })
.exec(function foundTransactions(err, transactions){
if(err)
return res.negotiate(err); // Send an appropriate error response back down to the client
// If there is no transactions, you can display a message in your view
return res.ok({ transactions: transactions }); // Send a 200 ("OK") response back down to the client with the provided data
});
}
下面是您如何编写相应的视图:
// Inside views/user/getUserTransaction.ejs
<% if(data.transactions.length) { %>
<!-- display each transactions... -->
...
<% } else { %>
<p>This user has no transaction yet.</p>
<% } %>
对于您的其他操作,这里是您如何使用 res.ok
和 pathToView
参数
res.ok({ hobbies: hobbies }, 'user/getUserTransactions');
您的视图必须位于 views/user/getUserTransactions.ejs
文件中才能 运行 正确。
现在我们已经审核了您的代码,可以回答您的问题了。
所以你想在另一个控制器中调用一个控制器的动作。这是 possible 但不推荐。
这是你应该做的:
// api/controllers/UserController.js
// In your getUserTransactions method (you should change its name)
// Get the user transactions
UserTransaction
.find({ user: req.param('userid') })
.exec(function foundTransactions(err, transactions) {
if(err) return res.negotiate(err);
// Get the user hobbies
UserHobby
.find({ user: req.param('userid') })
.exec(function foundHobbies(err, hobbies) {
if(err) return res.negotiate(err);
return res.ok({
transactions: transactions,
hobbies: hobbies
});
});
});
您还可以使用 services 来简化您的控制器。
// api/services/UserService.js
module.exports.getUserTransactions = function (userId, cb) {
UserTransaction
.find({ user: userId })
.exec(cb);
};
module.exports.getUserHobbies = function (userId, cb) {
UserHobby
.find({ user: userId })
.exec(cb);
};
// Inside api/controllers/UserController.js
UserService
.getUserTransactions(req.param('userid'), function (err, transactions) {
if(err) return res.negotiate(err);
UserService
.getUserHobbies(req.param('userid'), function (err, hobbies) {
if(err) return res.negotiate(err);
return res.ok({
transactions: transactions,
hobbies: hobbies
});
});
});
我还是 sails.js 的新手,目前正在尝试从不同的操作中获取 returned 对象。
目前我所知道的是,如果我在 'UserController' 中使用 'getUserTransaction' 操作,那么我只能 return 它到 'getUserTransaction.ejs' 使用当地人查看(_.each). (CMIIW) 示例:
//UserController
getUserTransaction: function (req, res, next){
UserTransaction.find({user:req.param('userid')}).exec(function foundTransactions(err, transactions){
if(err) return next(err);
if(!transactions) return next();
res.view({
transactions: transactions
});
});
}
//getUserTransaction.ejs
...
<% _.each(transactions, function(transaction){ %>
<tr>
<td><%= transaction.data %></td>
<td><a href="<%= transaction.path %>">Link</a></td>
</tr>
<% }); %>
...
但是,如果我想 return 从 'getUserHobby' 操作内部 'UserController' 对象到 'getUserTransaction.ejs' 视图怎么办?。 这是我的代码,但我做错了
//UserController
getUserHobby: function (req, res, next){
UserHobby.find({user:req.param('userid')}).exec(function foundHobbies(err, hobbies){
if(err) return next(err);
if(!hobbies) return next();
res.view('user/getUserTransaction', {
hobbies: hobbies
});
});
}
//getUserTransaction.ejs
...
<% _.each(hobbies, function(hobby){ %>
<tr>
<td><%= hobby.type %></td>
<td><%= hobby.name %></td>
</tr>
<% }); %>
...
我已经尝试这样做并且 returned 'hobbies undefined'。那么我该如何正确处理呢。
此致
首先,在 Sails controller 操作中,您不应使用第三个参数 (next
)。
Unlike Express middleware methods, Sails controller actions should always be the last stop in the request chain--that is, they should always result in either a response or an error. While it is possible to use
next
in an action method, you are strongly encouraged to use policies instead wherever possible.
你应该使用内置的response方法即使你想抛出一个错误。
以下是我编写控制器操作的方式:
// Inside api/controllers/UserController.js
getUserTransactions: function (req, res) {
if(!req.param('userid'))
return res.badRequest(); // Send a 400 response back down to the client indicating that the request is invalid
UserTransaction
.find({ user: req.param('userid') })
.exec(function foundTransactions(err, transactions){
if(err)
return res.negotiate(err); // Send an appropriate error response back down to the client
// If there is no transactions, you can display a message in your view
return res.ok({ transactions: transactions }); // Send a 200 ("OK") response back down to the client with the provided data
});
}
下面是您如何编写相应的视图:
// Inside views/user/getUserTransaction.ejs
<% if(data.transactions.length) { %>
<!-- display each transactions... -->
...
<% } else { %>
<p>This user has no transaction yet.</p>
<% } %>
对于您的其他操作,这里是您如何使用 res.ok
和 pathToView
参数
res.ok({ hobbies: hobbies }, 'user/getUserTransactions');
您的视图必须位于 views/user/getUserTransactions.ejs
文件中才能 运行 正确。
现在我们已经审核了您的代码,可以回答您的问题了。
所以你想在另一个控制器中调用一个控制器的动作。这是 possible 但不推荐。
这是你应该做的:
// api/controllers/UserController.js
// In your getUserTransactions method (you should change its name)
// Get the user transactions
UserTransaction
.find({ user: req.param('userid') })
.exec(function foundTransactions(err, transactions) {
if(err) return res.negotiate(err);
// Get the user hobbies
UserHobby
.find({ user: req.param('userid') })
.exec(function foundHobbies(err, hobbies) {
if(err) return res.negotiate(err);
return res.ok({
transactions: transactions,
hobbies: hobbies
});
});
});
您还可以使用 services 来简化您的控制器。
// api/services/UserService.js
module.exports.getUserTransactions = function (userId, cb) {
UserTransaction
.find({ user: userId })
.exec(cb);
};
module.exports.getUserHobbies = function (userId, cb) {
UserHobby
.find({ user: userId })
.exec(cb);
};
// Inside api/controllers/UserController.js
UserService
.getUserTransactions(req.param('userid'), function (err, transactions) {
if(err) return res.negotiate(err);
UserService
.getUserHobbies(req.param('userid'), function (err, hobbies) {
if(err) return res.negotiate(err);
return res.ok({
transactions: transactions,
hobbies: hobbies
});
});
});