DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Building Scalable Real-Time Apps with AstraDB and Vaadin
Register Now

Trending

  • How To Use an Automatic Sequence Diagram Generator
  • 21 Hidden Careers in the AI Revolution: Driving Change in the Tech Industry
  • Build a Simple Chat Server With gRPC in .Net Core
  • Test Data Tutorial: A Comprehensive Guide With Examples and Best Practices

Trending

  • How To Use an Automatic Sequence Diagram Generator
  • 21 Hidden Careers in the AI Revolution: Driving Change in the Tech Industry
  • Build a Simple Chat Server With gRPC in .Net Core
  • Test Data Tutorial: A Comprehensive Guide With Examples and Best Practices
  1. DZone
  2. Data Engineering
  3. Databases
  4. Playing with the Parse.com API

Playing with the Parse.com API

Hasin Hayder user avatar by
Hasin Hayder
·
Oct. 31, 13 · Interview
Like (0)
Save
Tweet
Share
7.67K Views

Join the DZone community and get the full member experience.

Join For Free


screen shot 2013-10-23 at 9.29.51 pm

i’ve started using the parse.com api not more than a week ago, and i've already fallen in love with it. the first thing that hit me was “whoa, no extra work for storing objects in my database” and that’s really it. saving data was never easier. parse.com’s javascript api, the documentation is very good and it will help you to start your project in no time. besides diving into the details, let me highlight another cool feature of parse.com api. it’s the user management. sign up someone, or let someone sign in, validate their email? everything is there!

’nuff said, how to start?

go to https://parse.com/apps , register a new application and copy the application key and javascript key.

now open your html file, add the parse.com’s js library

<script type="text/javascript" src="http://www.parsecdn.com/js/parse-1.2.12.min.js"></script>

and now initialize your parse.com application

parse.initialize("application id", "javascript key");

that’s it. now you are ready to play with the rest of this article.

sign me up, pronto!

one of the most tedious, boring work while developing an application is to deal with registration, email validation, acl and taking care of the signing in process. with parse.com api this is pretty straight forward. to sign up a new new user, all you gotta do is the following one!

var user = new parse.user();
user.set("username", "elmacho");
user.set("password", "chicken");
user.set("email", "el@macho.name");
 
user.signup(null, {
  success: function(user) {
    // everything's done!
    console.log(user.id);
  },
  error: function(user, error) {
    alert("error: " + error.code + " " + error.message);
  }
});

your user is signed up! if you need to validate user’s email then log into your parse.com dashboard and from your application’s settings section turn on the email validation. it’s simple.

all right, let me in!

alright, your users have signed up .now let them get in. it’s simple to write a sign in script with parse api

parse.user.login("elmacho", "chicken", {
  success: function(user) {
    console.log(user.get("email"));
  },
  error: function(user, error) {
    console.log("you shall not pass!");
  }
});

but hey, does a user have to log in every time? no, not at all. the logged in user’s state is saved in browser. so before attempting to sign in him/her again, you can check if he is already logged-in.

var loggedinuser = parse.user.current();
if(!loggedinuser){
    //off you go, perform a log in
} else {
    console.log(loggedinuser.get("email"));
}

easy, eh?

interesting, how can i deal with data?

okay, enough with signing up and signing in. lets save some data. say, for example, we want to store user’s favorite books in parse.com’s datastore. we will also retrieve the list later.

to save some data in parse.com, we need to write code like this

var user = parse.user.current();
var book = parse.object.extend("favoritebooks");
var b1 = new book({"name":"king solomon's mine","user":user});
b1.save();
 
var b2 = new book({"name":"return of she","user":user});
b2.save();

while saving an object you can also take care of the error or success state.

var b1 = new book({"name":"king solomon's mine","user":user});
b1.save(null, {
    success:function(b){
        console.log("successfully saved")
    },
    error:function(b,error){
        console.log(error.message);
    }
});

you can save as many books/data as you want, and link them up with the current user. linking like this is useful for retrieving them in future. as we have a few saved records, lets pull them and see if they are really saved.

var book = parse.object.extend("favoritebooks");
var q = new parse.query(book);
q.equalto("user",user);
q.find({
  success: function(results){
    for(i in results){
      var book = results[i];
      console.log(book.get("name"));
    }
  }
});

once it runs, you can see that it outputs currently logged in user’s favorite books. pretty neat, eh?

like equalto, there are a dozen of useful constraints which you can use with your query. you can get the complete list at https://parse.com/docs/js_guide#queries-constraints . parse.com’s documentation is pretty solid, and you will find almost everything over there.

it’s dinner time, log me out!

simple, just call parse.user.logout() and you are done!

so is it just about user management and saving data?

no, not at all!. but saving data and managing credentials + acl are one of the coolest features that you can do with parse.com api. beside these object storage, you can host your application (expressjs based), you can run your code in the cloud (background job, cloud code), take care of the push notifications and even upload and store binary files.

overall, parse.com’s api is super cool. give it a try today, you will love it for sure. and by the way, there is a super handy data browser in your application dashboard, use that!


API

Published at DZone with permission of Hasin Hayder, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • How To Use an Automatic Sequence Diagram Generator
  • 21 Hidden Careers in the AI Revolution: Driving Change in the Tech Industry
  • Build a Simple Chat Server With gRPC in .Net Core
  • Test Data Tutorial: A Comprehensive Guide With Examples and Best Practices

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: