gpt4 book ai didi

javascript - 如何从 sails.js 上的不同操作中获取返回的对象?

转载 作者:行者123 更新时间:2023-11-28 07:05:10 25 4
gpt4 key购买 nike

我对 sails.js 还很陌生,目前正在尝试从不同的操作中获取返回的对象。

目前我知道的是,如果我在“UserController”内使用“getUserTransaction”操作,那么我只能使用局部变量(_.each)将其返回到“getUserTransaction.ejs” View 。 (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>
<% }); %>
...

但是,如果我想将对象从“UserController”内的“getUserHobby”操作返回到“getUserTransaction.ejs” View 怎么办?这是我的代码,我无法正确执行

//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>
<% }); %>
...

我尝试这样做并返回“爱好未定义”。那么我应该如何做正确的事情。

问候

最佳答案

首先,在 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方法即使您想抛出错误

这是我编写 Controller 操作的方式:

// 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
});
}

以下是编写相应 View 的方法:

// 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');

您的 View 必须位于 views/user/getUserTransactions.ejs 文件中才能正确运行。

<小时/>

现在我们已经审核了您的代码,可以回答您的问题了。

所以你想在另一个 Controller 中调用 Controller 的操作。这是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简化您的 Controller 。

// 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
});
});
});

关于javascript - 如何从 sails.js 上的不同操作中获取返回的对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31768283/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com