我的 sails.js 服务 return “未定义”到调用控制器操作
My sails.js service return “undefined” to the calling controller action
我创建了一个名为 CategorieService 的服务。它的函数 getAllCategorie() 应该 return 一个对象:
//CategorieService.js
module.exports={
getAllCategorie:function()
{
Categorie.find({where:{statut:'A'},sort:'libelle ASC'}).exec(function(err,categories)
{
if(categories)
{
categories.forEach(function(categorie){
console.log('categorie libelle =>'+categorie.libelle);
})
return categories;
}
})
}
};
登录控制台显示结果随心所欲
categorie libelle => car
categorie libelle => clothes
categorie libelle => rent
但是在我的控制器中categorie is undefined
为什么?我该如何解决?在我的控制器下方
//ArticleControllerjs
var categorie=require('../services/CategorieService');
module.exports = {
/*view*/
indexArticle:function(req,res)
{
var title=req.__('gestiondesarticles.title');
res.view('article',{categories:categorie.getAllCategorie(),title:title,page_name:'article'});
},
}
因为所有的数据库访问都是异步的,所以不能使用categorie.getAllCategorie()
,需要使用回调
你需要做的:
//CategorieService.js
module.exports={
getAllCategorie:function(cb)
{
Categorie.find({where:{statut:'A'},sort:'libelle ASC'}).exec(function(err,categories)
{
if(categories)
{
categories.forEach(function(categorie){
console.log('categorie libelle =>'+categorie.libelle);
})
cb(null, categories);
}
else {
cb(err, null);
}
})
}
};
和
//ArticleControllerjs
module.exports = {
/*view*/
indexArticle:function(req,res)
{
var title=req.__('gestiondesarticles.title');
CategorieService.getAllCategorie(function(err, categories){
if(categories)
{
res.view('article',{categories:categories,title:title,page_name:'article'});
}
});
},
}
PS : 不需要你的服务,Sails 已经为你提供了它(除非你禁用它)
我创建了一个名为 CategorieService 的服务。它的函数 getAllCategorie() 应该 return 一个对象:
//CategorieService.js
module.exports={
getAllCategorie:function()
{
Categorie.find({where:{statut:'A'},sort:'libelle ASC'}).exec(function(err,categories)
{
if(categories)
{
categories.forEach(function(categorie){
console.log('categorie libelle =>'+categorie.libelle);
})
return categories;
}
})
}
};
登录控制台显示结果随心所欲
categorie libelle => car
categorie libelle => clothes
categorie libelle => rent
但是在我的控制器中categorie is undefined
为什么?我该如何解决?在我的控制器下方
//ArticleControllerjs
var categorie=require('../services/CategorieService');
module.exports = {
/*view*/
indexArticle:function(req,res)
{
var title=req.__('gestiondesarticles.title');
res.view('article',{categories:categorie.getAllCategorie(),title:title,page_name:'article'});
},
}
因为所有的数据库访问都是异步的,所以不能使用categorie.getAllCategorie()
,需要使用回调
你需要做的:
//CategorieService.js
module.exports={
getAllCategorie:function(cb)
{
Categorie.find({where:{statut:'A'},sort:'libelle ASC'}).exec(function(err,categories)
{
if(categories)
{
categories.forEach(function(categorie){
console.log('categorie libelle =>'+categorie.libelle);
})
cb(null, categories);
}
else {
cb(err, null);
}
})
}
};
和
//ArticleControllerjs
module.exports = {
/*view*/
indexArticle:function(req,res)
{
var title=req.__('gestiondesarticles.title');
CategorieService.getAllCategorie(function(err, categories){
if(categories)
{
res.view('article',{categories:categories,title:title,page_name:'article'});
}
});
},
}
PS : 不需要你的服务,Sails 已经为你提供了它(除非你禁用它)