基于(帆文档)[http://sailsjs.org/documentation/concepts/services/creating-a-service],我可以在里面services
文件夹这样做创建一个服务:无法访问资源变量上帆影服务
// EmailService.js - in api/services
module.exports = {
sendInviteEmail: function(options) {
var opts = {"type":"messages","call":"send","message":
{
"subject": "YourIn!",
"from_email": "[email protected]",
"from_name": "AmazingStartupApp",
"to":[
{"email": options.email, "name": options.name}
],
"text": "Dear "+options.name+",\nYou're in the Beta! Click <insert link> to verify your account"
}
};
myEmailSendingLibrary.send(opts);
}
};
所以选项收到来自控制器的参数,可以,但我不知道为什么这应该工作,因为myEmailSendingLibrary
根本没有定义。
此外,我想在服务内部实现ORM(水线)查找方法。基于帆文档,必须是这样的:
,当我试图加入所有的问题就来了。如果我创建了以下服务称为DesignerService.js
:
module.exports = {
findFormIds: function(id) {
Designer.find("56dedc1734112e8c1fb078aa").exec(function (err, records) {
//}).exec(function (err, finn){
if (err) {
return res.negotiate(err);
}
if (!records) {
return res.notFound('Could not find any form.');
}
//sails.log('Found "%s"', records.fullName);
myFindFormIds.json(records);
//return res.json(records);
});
}
};
,因为没有创建资源这是行不通的。如果我试图以宣布一个函数来获取这样的访问资源变量:
module.exports = function(req,res){
findFormIds: function(id) {
Designer.find("56dedc1734112e8c1fb078aa").exec(function (err, records) {
//}).exec(function (err, finn){
if (err) {
return res.negotiate(err);
}
if (!records) {
return res.notFound('Could not find any form.');
}
//sails.log('Found "%s"', records.fullName);
myFindFormIds.json(records);
//return res.json(records);
});
}
};
我对在findFormIds线意外(
错误。
那么我怎样才能访问res
var以便发回查询结果呢?
谢谢!
您需要将'res'对象从控制器动作传递给服务 – elssar