javascript - How to implement custom routes in sailsjs -


i'm trying understand sails.js. firstly try generate new api. code of models , controller

coutries.js

module.exports = {    attributes: {     title:{         type:'string',         unique:true,         primarykey:true     },      cities:{         collection:'cities',         via:'country'     }   } }; 

cities,js

module.exports = {    attributes: {     title: {       type:'string',       unique:true,       primarykey:true     },     country:{         model:'countries',         type:'string'     },     description: {         type:'string'     }   } }; 

next in routes.js wrote

'get /countries':'countriescontroller.getallcountries' 

i need list of countries. can not understand how implement function getallcountries in countriescontroller.js. use local db in tmp directory. please, can understand me in details how can this? understanding tell me how implement function addnewcountries , example updatecitiesdescription.

thanks , sorry english)

well, if aim view list of countries sails has got covered. provides blueprint api's can directly use query db , view response json.

for example, can call

http://localhost:1337/countries 

to view countries in db. can carry out other queries directly using api. find more info here

however, if still query database hang of it, you've done far on right track.

in countriescontroller, create new action called "getallcountries"

getallcountries: function(req, res) {    countries.find().exec(function(err, countries){      res.json(countries);    }); }); 

your route tries find method named "getallcountries" in controller "countriescontroller" , redirects request action.

upon receiving call, fetch list of countries database using waterline's query language , return json user.

a friendly advice, avoid naming models in plural. example, if trying maintain countries , cities in db, name models "country" , "city".


Comments

Popular posts from this blog

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -

YouTubePlayerFragment cannot be cast to android.support.v4.app.Fragment -