dimanche 19 avril 2015

My sailsjs service return “undefined” to the calling controller action

I created a service called CategorieService. Its function getAllCategorie() is supposed to return an object:



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


log in console show result as I want



categorie libelle => car
categorie libelle => clothes
categorie libelle => rent


But When in my controllercategorie is undefined why ? and How can I fix it ? below my controller



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

},
}

Aucun commentaire:

Enregistrer un commentaire