How to implement custom routes in sailsjs

I'm trying to understand sails.js. Firstly I try generate new api. This is my code of models and 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 I wrote

'get /countries':'CountriesController.getAllCountries'

I need a list of countries. I can not understand how to implement the function getAllCountries in CountriesController.js. I use a local db in tmp directory. Please, can somebody understand me in details how I can do this? Also for understanding tell me how to implement a function addNewCountries and for example updateCitiesDescription.

Thanks and sorry for my english)

Upvotes: 3

Views: 91

Answers (2)

Bwaxxlo
Bwaxxlo

Reputation: 1880

With sails, if you have a function to handle your routes, you create it inside api/controllers/CountriesController as a method.

For routes:

'get /countries':'CountriesController.getAllCountries'

Countries Controller:

module.exports = {
  getAllCountries: function (req, res, next){
    return Country.getAllCountries(function success(countries){
      //handle your success logic e.g: return res.view("countries", {data: countries});
    }, function errorHandle(error){
      //error handling
    })
  }

}

Countries Model:

//inside your module.exports in api/models/Countries add this method
getAllCountries: function(cb, err){
  Country.find({}).then(cb).catch(err);
}

To summarize, you use your controller method to contact the model and pass a success and error handling functions that will return an appropriate view.

Upvotes: 2

Prince Shekhar
Prince Shekhar

Reputation: 119

Well, if your aim is to just view the list of countries then Sails has got you covered. It provides blueprint API's which you can directly use to query your db and view the response as JSON.

For example, you can call

http://localhost:1337/countries

to view all the countries in the db. Similarly you can carry out other queries as well directly using the API. Find more info about it here

However, if you would still like to query the database yourself to get the hang of it, what you've done so far is on the right track.

In your CountriesController, create a new action called "getAllCountries"

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

Your route basically tries to find a method named "getAllCountries" in the controller "CountriesController" and redirects your request to that action.

Upon receiving the call, you just fetch the list of countries from the database using waterline's query language and return the JSON back to the user.

A friendly advice, avoid naming your models in plural. For example, if you are trying to maintain countries and cities in your db, then name the models "Country" and "City".

Upvotes: 1

Related Questions