Saturday 19 September 2015

How to use parse db from your node application


Here I am going through the simple steps to demonstrate how to access parse DB from
your node application.

To know more about parse db please visit www.parse.com

If you have your node based web app ready, you can include parse db in your app in two ways

1. Java Script.

Step 1: Include parse script as a bower_component. Use below command for this.
        bower install parse
Step 2: Create a test class in parse db.
        Register in parse.com and create a sample test app db with some columns.
       After creation of your tables, get the keys for external use from settings page.
Step 3: Initialize parse.
                      Parse.initialize('application id','Java script key');
Step 4: Querying the parse db from script

        Here I am going to query the data from Test class in below function
$scope.getLocation = function(val) {
    var locations=[];
    var Etest = Parse.Object.extend("Your class name from parse DB");
    var query = new Parse.Query(Etest); 
//add filters in your query
    query.startsWith("name", val.toLowerCase());
//querying
   return query.find({
        success: function(results) {
     
        },
         error: function(error) {
       alert("Error: " + error.code + " " + error.message);
      }
     }).then(function(results){
   
//returning the results to front end
      for (var i = 0; i < results.length; i++) {
           var object = results[i];
           locations.push(object.get('name')
          }
      return locations;
     });


2. As an $http Rest request.

  For accessing the parse db as a rest api request , we don't need to use parse script.
Step 1:We need to add below information to request header.
$http.defaults.headers.common['X-Parse-Application-Id']
    ='Application Id';
    $http.defaults.headers.common['X-Parse-REST-API-Key']
    ='Rest Api Key';

Step 2: Accessing from your java script function.
We need to pass $htttp to your controller script
$scope.data = {};
$http.get('https://api.parse.com/1/classes/etest')
    .success(function (data){       
     
    $scope.data.test =data.results;
    console.log('data'+$scope.data.test);
    })
    .error(function (response){
        $scope.data.error = response.error || response;
    });